this

template

The template keyword is used to create generic functions and classes. The type of data operated upon by a generic function or class is specified as a parameter. Thus, one function or class definition can be used with several different types of data. The details concerning template functions and classes follow.

A generic function defines a general set of operations that can be applied to various types of data. A generic function has the type of data that it will operate upon passed to it as a parameter. Using this mechanism, the same general procedure can be applied to a wide range of data. As you know, many algorithms are logically the same no matter what type of data is being operated upon. For example, the Quicksort algorithm is the same whether it is applied to an array of integers or an array of floating-point numbers. It is just that the type of the data being sorted is different. By creating a generic function, you can define, independent of any data, the nature of the algorithm. Once this is done, the compiler automatically generates the correct code for the type of data that is actually used when you call the function. In essence, when you create a generic function you are creating a function that can automatically overload itself.

The general form of a template function definition is shown here:

 template <class data-type> ret-type func-name(parameter list) {      // body of function }

Here, data-type is a placeholder for the type of data upon which the function will actually operate. You can define more than one generic data type by using a comma-separated list.

Here is an example. The following program creates a generic function that swaps the values of the two variables with which it is called. Because the general process of exchanging two values is independent of the type of the variables, it is a good candidate to be made into a generic function.

// Function template example. #include <iostream> using namespace std; // Here is a template function. template <class X> void swapvals(X &a, X &b) {   X temp;   temp = a;   a = b;   b = temp; } int main() {   int i=10, j=20;   float x=10.1, y=23.3;   cout << "Original i, j: " << i << ' ' << j         << endl;   cout << "Original x, y: " << x << ' ' << y         << endl;   swapvals(i, j); // swap integers   swapvals(x, y); // swap floats   cout << "Swapped i, j: " << i << ' ' << j         << endl;   cout << "Swapped x, y: " << x << ' ' << y         << endl;   return 0; }

In this program, the line,

template <class X> void swapvals(X &a, X &b)

tells the compiler two things: first, that a template function is being created, and second, that X is a generic type that is used as a placeholder. The body of swapvals( ) is defined using X as the data type of the values that will be swapped. In main( ), the swapvals( ) function is called using two different types of data: integers and floating-point numbers. Because swapvals( )
is a generic function, the compiler automatically creates two versions of swapvals( )—one that will exchange integer values and one that will exchange floating-point values.

Generic functions are similar to overloaded functions except that they are more restrictive. When functions are overloaded, you can have different actions performed within the body of each function. A generic function must perform the same general action for all versions.

In addition to generic functions, you can also define a generic class. When you do this, you create a class that defines all algorithms used by that class, but the actual type of the data being manipulated will be specified as a parameter when objects of that class are created.

Generic classes are useful when a class contains generalizable logic. For example, the same algorithm that maintains a queue of integers will also work for a queue of characters. Also, the same mechanism that maintains a linked list of mailing addresses will also maintain a linked list of auto parts. By using a generic class, you can create a class that will maintain a queue, linked list, and so on for any type of data. The compiler will automatically generate the correct type of object based upon the type you specify when the object is created.

Here is the general form of a generic class declaration:

 template <class data_type> class class-name {      // ... };

In this case, data_type is a placeholder for a type of data upon which the class will operate. You can define more than one generic data type by using a comma-separated list. When you declare an object of a generic class, you specify the type of data between angle brackets, using this general form:

class-name<type> object;

The following is an example of a generic class. This program creates a very simple generic singly linked list class. It then demonstrates the class by creating a linked list that stores characters.

// A simple generic linked list. #include <iostream> using namespace std; template <class data_t> class list {   data_t data;   list *next; public:   list(data_t d);   void add(list *node) { node->next = this;                           next = 0; }   list *getnext() { return next; }   data_t getdata() { return data; } }; template <class data_t> list<data_t>::list(data_t d) {   data = d;   next = 0; } int main() {   list<char> start('a');   list<char> *p, *last;   int i;   // build a list   last = &start;   for(i=0; i<26; i++) {     p = new list<char> ('a' + i);     p->add(last);     last = p;   }   // follow the list   p = &start;   while(p) {     cout << p->getdata();     p = p->getnext();   }   return 0; } 

As you can see, the declaration of a generic class is similar to that of a generic function. The actual type of data stored by the list is made generic in the class declaration. In main( ), objects and pointers are created that specify that the data type of the list will be char.

Pay special attention to this declaration:

list<char> start('a');

Notice how the desired data type is passed inside the angle brackets.




C(s)C++ Programmer's Reference
C Programming on the IBM PC (C Programmers Reference Guide Series)
ISBN: 0673462897
EAN: 2147483647
Year: 2002
Pages: 539

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net