Template Definition in C++


Templates allow you to teach the compiler how to make a list of any type of thing, rather than creating a set of type-specific lists--a PartsList is a list of parts, a CatList is a list of cats. The only way in which they differ is the type of the thing on the list. With templates, the type of the thing on the list becomes a parameter to the definition of the class.

A common component of virtually all C++ libraries is an array class. As you saw with Lists, it is tedious and inefficient to create one array class for integers, another for doubles, and yet another for an array of Animals. Templates let you declare a parameterized array class and then specify what type of object each instance of the array will hold.

New Term: Instantiation is the act of creating a specific type from a template. The individual classes are called instances of the template.
Parameterized templates provide you with the ability to create a general class, and pass types as parameters to that class, in order to build specific instances.

Template Definition

You declare a parameterized Array object (a template for an array) by writing
1: template <class T>    // declare the template and the parameter
2: class Array           // the class being parameterized
3: {
4:    public:
5:      Array();
6:   // full class declaration here
7: };
The keyword template is used at the beginning of every declaration and definition of a template class. The parameters of the template are after the keyword template. The parameters are the things that will change with each instance. For example, in the array template shown previously, the type of the objects stored in the array will change. One instance might store an array of integers, while another might store an array of Animals.

In this example, the keyword class is used, followed by the identifier T. The keyword class indicates that this parameter is a type. The identifier T is used throughout the rest of the template definition to refer to the parameterized type. One instance of this class will substitute int everywhere T appears, and another will substitute Cat.

To declare an int and a Cat instance of the parameterized Array class, you would write
Array<int> anIntArray;
Array<Cat> aCatArray;
The object anIntArray is of the type array of integers; the object aCatArray is of the type array of cats. You can now use the type Array<int> anywhere you would normally use a type--as the return value from a function, as a parameter to a function, and so forth. Listing 19.1 provides the full declaration of this stripped-down Array template.

NOTE: Listing 19.1 is not a complete program!

Listing 19.1. A template of an Array class

1: Listing 19.1 A template of an array class
2: #include <iostream.h>
3: const int DefaultSize = 10;
4:
5: template <class T> // declare the template and the parameter
6: class Array // the class being parameterized
7: {
8: public:
9: // constructors
10: Array(int itsSize = DefaultSize);
11: Array(const Array &rhs);
12: ~Array() { delete [] pType; }
13:
14: // operators
15: Array& operator=(const Array&);
16: T& operator[](int offSet) { return pType[offSet]; }
17:
18: // accessors
19: int getSize() { return itsSize; }
20:
21: private:
22: T *pType;
23: int itsSize;

24: };


Output: There is no output. This is an incomplete program.
Analysis: The definition of the template begins on line 5, with the keyword template followed by the parameter. In this case, the parameter is identified to be a type by the keyword class, and the identifier T is used to represent the parameterized type.
From line 6 until the end of the template on line 24, the rest of the declaration is like any other class declaration. The only difference is that wherever the type of the object would normally appear, the identifier T is used instead. For example, operator[] would be expected to return a reference to an object in the array, and in fact it is declared to return a reference to a T.
When an instance of an integer array is declared, the operator= that is provided to that array will return a reference to an integer. When an instance of an Animal array is declared, the operator= provided to the Animal array will return a reference to an Animal.

Using the Name

Within the class declaration, the word Array may be used without further qualification. Elsewhere in the program, this class will be referred to as Array<T>. For example, if you do not write the constructor within the class declaration, you must write
template <class T>
Array<T>::Array(int size):
itsSize = size
{
pType = new T[size];
for (int i = 0; i<size; i++)
pType[i] = 0;
}
The declaration on the first line of this code fragment is required to identify the type (class T). The template name is Array<T>, and the function name is Array(int size).

The remainder of the function is exactly the same as it would be for a non-template function. It is a common and preferred method to get the class and its functions working as a simple declaration before turning it into a template.
Share this article :
 
Copyright © 2012. Best Online Tutorials | Source codes | Programming Languages - All Rights Reserved