10.5. Containment with Template Classes

Containment (page 223) is an important design technique because of code reuse and data encapsulation. With containment, classes have data members that are objects of another class. Containment applies to template classes with the following format.

template <class TYPE> 
class A {                                 // template class
private:
   B<TYPE> member1;                       // generic template member
   C<type> member2;                       // specific template member
   D member3;                             // nontemplate member
public:
   A(args) : B(args),C(args),D(args) { }     // member initialization
. . .
};

Inside template class A, you may instantiate generic template members (B), specific types for template members (C), and nontemplate members (D). The compiler uses TYPE from the template parameter list ...

Get Navigating C++ and Object-Oriented Design 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.