Examples of Templates

Given the following template:

template<class L> class Key
{
   L k;
    L* kptr;
    int length;
public:
    Key(L);
    // ...
};

The following table shows what the classes Key<int>, Key< char*>, and Key<mytype> look like:

/------------------------------------------------------------------------------\
| class Key<int> i;       | class Key<char*> c;      | class Key<mytype> m;    |
|-------------------------+--------------------------+-------------------------|
| class Key<int> {        | class Key<char*> {       | class Key<mytype> {     |

|       int k;            |       char* k;           |       mytype k;         |
|       int * kptr;       |       char** kptr;       |       mytype* kptr;     |
|       int length;       |       int length;        |       int length;       |
| public:                 | public:                  | public:                 |
|       Key(int);         |       Key(char*);        |       Key(mytype);      |
|       // ...  };        |       // ...  };         |       // ...  };        |
\------------------------------------------------------------------------------/

The declarations create the following objects:


Note that these three classes have different names. The types contained within the angle braces are not arguments to the class names, but part of the class names themselves. Key<int> and Key<char*> are class names. Within the context of the example, a class called Key (with no template argument list) is undefined.