16.4.1 class template member functions
The class template member functions are defined as follows:
It must start with the keyword template, followed by the template parameter table of the class.
You must specify the member of the class.
The class name must contain its template parameters.
1. destroy Function
Template <class Type> void Queue <Type >:: destroy (){
While (! Empty ())
Pop ();
}
Template <class Type> void Queue <Type >:: destroy (){
While (! Empty ())
Pop ();
} 2. pop Function
Template <class Type> void Queue <Type>: pop ()
{
QueueItem <Type> p = head;
This-> head = this-> head-> next;
Delete p;
}
Template <class Type> void Queue <Type>: pop ()
{
QueueItem <Type> p = head;
This-> head = this-> head-> next;
Delete p;
} 3. push Function
Template <class Type> void Queue <Type>: push (const Type & p)
{
QueueItem <Type> * pt = new QueueItem <Type> (p );
If (empty ())
Head = tail = pt;
Else {
This-> tail-> next = pt;
Tail = pt;
}
}
Template <class Type> void Queue <Type>: push (const Type & p)
{
QueueItem <Type> * pt = new QueueItem <Type> (p );
If (empty ())
Head = tail = pt;
Else {
This-> tail-> next = pt;
Tail = pt;
}
} 4. copy_elems Function
Template <class Type>
Void Queue <Type >:: copy_elems (const Queue & orig ){
For (QueueItem <Type> * pt = orig. head; pt = 0; pt = pt-> next)
{
Push (pt-> item );
}
}
Template <class Type>
Void Queue <Type >:: copy_elems (const Queue & orig ){
For (QueueItem <Type> * pt = orig. head; pt = 0; pt = pt-> next)
{
Push (pt-> item );
}
} 5. instantiation of class template member functions
The member functions of the class template are also function templates. Like any other function template, you need to use the member functions of the class template to generate the instantiation of the member. Unlike other function templates, when instantiating class template member functions, the compiler does not execute template real-parameter inference. On the contrary, the template parameters of the class template member function are determined by the type of the object that calls the function.
6. When to instantiate classes and members
The member functions of the class template are instantiated only when they are used by the program. If a function is never used, the member function is not instantiated.
From xufei96's column