O'Reilly logo

Navigating C++ and Object-Oriented Design by Gail Anderson, Paul Anderson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required