4.2 Template superclasses

A template does not need to use the template parameter as the type of a member. The parameter of a template can be used to specify a superclass! This is illustrated in listing 8.


Listing 8:templatesuper
 
1template <class T> class X : public T 
2{ 
3  // ... 
4};

This approach gives us the flexibility to specify how to extend a base class as a class template, then apply the extension to different superclasses. In our example, let us consider the template class X<LinkedList<int> > (note the space between the two > symbols so it is not misunderstood as >>). This template class has all the methods by LinkedList<int>, but it also contains the extensions specified by X.

It is also possible to specify a class template as a subclass of another class template. This is illustrated in listing 9.


Listing 9:templatesuper
 
1template <class T> class X 
2{ 
3  // ... 
4}
5 
6template <class T> class Y : public X<T> 
7{ 
8  // ... 
9};