// Message: ‘C2’

is used as a type, but

 

// has

not been defined as a type.

typename T::C2 *p;

//

Solution: the

keyword typename flags

 

//

the

qualified

name T::C2 as a type.

};

 

 

 

 

class C {

//details omitted class C2 {

//details omitted

};

};

int main ()

{

C1<C> c;

}

In a template, a name is not taken to be a type unless it is explicitly declared as one. Ways to declare a name as a type include:

Use it as the argument to the template (T below):

template<class T> class C {

// Additional details omitted

};

Use it as the name of the template (C below):

template<class T> class C {

// Additional details omitted

};

Declare a class as a member of the class template (C2 below):

template<class T> class C1 {

class C2;

// Additional details omitted

};

Declare a class in the context the template is declared within (C1 below): class C1;

template<class T> class C2 {

// details omitted

};

Overloading new[] and delete[] for Arrays

HP aC++ defines new and delete operators for arrays that are different from those used for single objects. These operators, operator new[ ] ( ) and operator delete[ ] ( ), can be overloaded both globally, and in a class. If you use operator new( ) to allocate memory for a single object, you should use operator delete( ) to deallocate this memory. If you use operator new[ ] ( ) to allocate an array, you should use operator delete[ ] ( ) to deallocate it.

Usually, the allocation and deallocation of operators is overloaded for a particular class, not globally. This overloading allows you to put all instances of a particular class on a class-specific heap. You can then take control of allocation either for efficiency or to accomplish other storage management functions, for example garbage collection. If allocation and deallocation of single objects is overloaded, you may or may not want to overload the operators for arrays. If the

150 Standardizing Your Code