Chapter 2 templates and generic programming (8)

Source: Internet
Author: User

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
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.