Template Versatility

You can apply the same techniques to template classes as you do to regular classes. Template classes can serve as base classes, and they can be component classes. They can themselves be type arguments to other templates. For example, you can implement a stack template by using an array template. Or you can have an array template that is used to construct an array whose elements are stacks based on a stack template. That is, you can have code along the following lines:

template <typename T>   // or <class T>class Array{private:    T entry;    ...};template <typename Type>class GrowArray : public Array<Type> {...};  // inheritancetemplate <typename Tp>class Stack{       Array<Tp> ar;       // use an Array<> as a component

Get C++ Primer Plus now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.