...
};
Here T is the type parameter, and it acts as a stand-in for a real type to be specified at a later time. (This parameter can have any valid C++ name, but T and Type are common choices.) You can also use typename instead of class in this context:
template
class Rev {...} ;
Class definitions (instantiations) are generated when you declare a class object and specify a particular type. For example, the following declaration causes the compiler to generate a class declaration in which every occurrence of the type parameter T in the template is replaced by the actual type short in the class declaration:
class Ic
In this case, the class name is Ic
An explicit instantiation occurs when you declare a specific specialization of the class, using the keyword template:
template class IC
In this situation, the compiler uses the general template to generate an int specialization Ic
You can provide explicit specializations, which are specialized class declarations that override a template definition. You just define the class, starting with template<>, and then you use the template class name, followed by angle brackets containing the type for which you want a specialization. For example, you could provide a specialized Ic class for character pointers as follows:
template <> class Ic
{
char * str;
...
public:
Ic(const char * s) : str(s) { }
...
};
Then a declaration of the following form would use the specialized definition for chic rather than using the general template:
class Ic
A class template can specify more than one generic type and can also have non-type parameters:
template
class Pals {...};
The following declaration would generate an implicit instantiation using double for T, string for TT, and 6 for n:
Pals
A class template can also have parameters that are templates:
template < template
class Trophy {...};
Here z stands for an int value, U stands for the name of a type, and CL stands for a class template declared using template
Class templates can be partially specialized:
template
template
template
The first example here creates a specialization in which both types are the same and n has the value 6. Similarly, the second creates a specialization for n equal to 100, and the third creates a specialization for which the second type is a pointer to the first type.
Template classes can be members of other classes, structures, and templates.
The goal of all these methods is to allow programmers to reuse tested code without having to copy it manually. This simplifies the programming task and makes programs more reliable.
Chapter Review
1. For each of the following sets of classes, indicate whether public or private derivation is more appropriate for Column B:
2. Suppose you have the following definitions:
class Frabjous {
private:
char fab[20];
public:
Frabjous(const char * s = "C++") : fab(s) { }
virtual void tell() { cout << fab; }
};
class Gloam {
private:
int glip;
Frabjous fb;
public:
Gloam(int g = 0, const char * s = "C++");
Gloam(int g, const Frabjous & f);
void tell();
};
Given that the Gloam version of tell() should display the values of glip and fb, provide definitions for the three Gloam methods.
3. Suppose you have the following definitions:
class Frabjous {
private:
char fab[20];
public:
Frabjous(const char * s = "C++") : fab(s) { }
virtual void tell() { cout << fab; }
};
class Gloam : private Frabjous{
private:
int glip;
public:
Gloam(int g = 0, const char * s = "C++");
Gloam(int g, const Frabjous & f);
void tell();
};
Given that the Gloam version of tell() should display the values of glip and fab, provide definitions for the three Gloam methods.
4. Suppose you have the following definition, based on the Stack template of Listing 14.13 and the Worker class of Listing 14.10:
Stack
Write out the class declaration that will be generated. Just do the class declaration, not the non-inline class methods.
5. Use the template definitions in this chapter to define the following:
• An array of string objects
• A stack of arrays of double
• An array of stacks of pointers to Worker objects
How many template class definitions are produced in Listing 14.18?
6. Describe the differences between virtual and nonvirtual base classes.
Programming Exercises