Previous 2D int Array





Step 1: Create Templated Version





Step 2: Use Nested Classes





Nested Classes

If class ClassB is nested in ClassA:





Step 3: Nested Class Defined Outside

Class definitions inside another class definition look clunky.

Declare ClassB inside ClassA but define it elsewhere:

class ClassA { public: class ClassB ; ... } ; class ClassA::ClassB { ... } ;



Step 4: Templated Nested Classes

Define SmartPointer as a template member of TwoDimArray.

template <class T> class TwoDimArray { public: ... template <class TYPE> class SmartPtr { ... } ; ... } ;



Templated Nested Classes





Step 5: "Non-templated" Member Classes





The typename problem

These two pieces of code look the same to the compiler:
template <class T> void func(T& item) { T::foo *ptr ; ... } template <class T> void func(T& item) { T::foo * factor ; ... }

What does T::foo * bar mean?

Is it a pointer definition? or multiplication?




Using the typename Keyword





Step 6: Overloading the << Operator