Question
What's the syntax / semantics for a "class template"?

Answers

Consider a container class Array that acts like an array of integers:

// This would go into a header file such as "Array.h"
class Array {
public:
Array(int len=10) : len_(len), data_(new int[len]) { }
~Array() { delete[] data_; }
int len() const { return len_; }
const int& operator[](int i) const { return data_[check(i)]; } ← subscript operators often come in pairs
int& operator[](int i) { return data_[check(i)]; } ← subscript operators often come in pairs
Array(const Array&);
Array& operator= (const Array&);
private:
int len_;
int* data_;
int check(int i) const
{ if (i < 0 || i >= len_) throw BoundsViol("Array", i, len_);
return i; }
};

Repeating the above over and over for Array of float, of char, of std::string, of Array-of-std::string, etc, will become tedious.

// This would go into a header file such as "Array.h"
template
class Array {
public:
Array(int len=10) : len_(len), data_(new T[len]) { }
~Array() { delete[] data_; }
int len() const { return len_; }
const T& operator[](int i) const { return data_[check(i)]; }
T& operator[](int i) { return data_[check(i)]; }
Array(const Array&);
Array& operator= (const Array&);
private:
int len_;
T* data_;
int check(int i) const
{ if (i < 0 || i >= len_) throw BoundsViol("Array", i, len_);
return i; }
};

Unlike template functions, template classes (instantiations of class templates) need to be explicit about the parameters over which they are instantiating:

int main()
{
Array ai;
Array af;
Array ac;
Array as;
Array< Array > aai;
...
}

Note the space between the two >'s in the last example. Without this space, the compiler would see a >> (right-shift) token instead of two >'s.   Your Comment




More Software Questions..
What is the inputsplit in map reduce software?

What is software configuration management?

What Is Java Api For Xml-based Rpc (jax-rpc)?

How can you implement fine-grained auditing?

What is IBM’s simple explanation for Big Data’s four critical features?

What is static synchronized method in JDBC API? Give an example?

What does the NULLIF function do?

What happens if a start method is not invoked and the run method is directly invoked?

Should we override finalize method

what is the difference between mysql_fetch_array and mysql_fetch_object?

How will XML affect my document links?

Why to use Style Sheets?

What are Filters in MVC?

Can you explain Application layer in OSI model?

How to define new testplan attributes?

What are the minimum system requirements to run Photoshop? Is it possible to run Photoshop over linux?

Which oracle package is used to manage the oracle lock management services?

What is Latch Up? Explain Latch Up with cross section of a CMOS Inverter. How do you avoid Latch Up?

What is marker interface?

What types of partitioning are there for BW?



Search
Can you Answer!!
  • Q What country has the international car registration letters TR?
  • Q What does the boys name Paul mean
  • Q Which Mohghul King sat on the throne with title of " Padshah Gazi" ?
  • Q Latency problems: myth or reality?
  • Q Write Notes on (a) Scenarios (b) Data – subtotals
  • Q What is a function in SQL Server?
  • Q What is the difference between foreign key and reference key ?
  • Q What are sales?
  • Q What is Vaccum ?
  • Q Can a thread call a non-synchronized instance method of an Object when a synchronized method is being executed?
  • Q How will you move mappings from development to production database?