Chapter 2 templates and generic programming (11)

Source: Internet
Author: User

16.4.5 member Template

Any class (template or non-template) can have its own members as class templates or function templates. Such members are called member templates, and member templates cannot be virtual.

1. Define a member Template


Template <class Type>
Class Queue {
Public:
Queue (): head (0), tail (0 ){}
Queue (const Queue & Q): head (0), tail (0)
{Copy_elems (Q );}
Queue & operator = (const Queue &);
~ Queue () {destroy ();}
Type & front ()
{Return head-> item ;}
Const Type & front () const {return head-> item ;}
Void push (const Type &);
Void pop ();
Bool empty () const
{Return head = 0 ;}
Friend ostream & operator <Type> (ostream & OS, const Queue <Type> & q );
 
Template <class It>
Queue (It beg, It end): head (0), tail (0) {copy_elems <It> (beg, end )}
Template <class Iter>
Void assign (Iter, Iter );
Private:
QueueItem <Type> * head;
QueueItem <Type> * tail;
Void destroy ();
Void copy_elems (const Queue &);
Template <class Iter>
Void copy_elems (Iter, Iter );
};
Template <class Type>
Class Queue {
Public:
Queue (): head (0), tail (0 ){}
Queue (const Queue & Q): head (0), tail (0)
{Copy_elems (Q );}
Queue & operator = (const Queue &);
~ Queue () {destroy ();}
Type & front ()
{Return head-> item ;}
Const Type & front () const {return head-> item ;}
Void push (const Type &);
Void pop ();
Bool empty () const
{Return head = 0 ;}
Friend ostream & operator <Type> (ostream & OS, const Queue <Type> & q );

Template <class It>
Queue (It beg, It end): head (0), tail (0) {copy_elems <It> (beg, end )}
Template <class Iter>
Void assign (Iter, Iter );
Private:
QueueItem <Type> * head;
QueueItem <Type> * tail;
Void destroy ();
Void copy_elems (const Queue &);
Template <class Iter>
Void copy_elems (Iter, Iter );
}; 2. Define a member template outside the class

When defining a member template outside the scope of the class template, it must contain two template parameters.


Template <class Type>
Template <class Iter>
Void Queue <Type >:: assign (Iter beg, Iter end ){
Destroy ();
Copy_elems (beg, end );
}
Template <class Type>
Template <class Iter>
Void Queue <Type >:: assign (Iter beg, Iter end ){
Destroy ();
Copy_elems (beg, end );
} Because the assign function deletes members from an existing container, it is necessary for the iterator passed to the assign function to reference elements in different containers. The assign members of the standard container have the same restrictions as the iterator constructor.

3. The member template follows regular access control

The member template follows the same access rules as any other class members.

4. Member templates and instantiation

Like other members, a member template is instantiated only when used in a program. A member template has two template parameters: defined by the class and defined by the member template itself. The class template parameters are determined by the type of the object that calls the function. The behavior of the member-defined template parameters is the same as that of the common function template. These parameters are determined by the real parameters of the general template.

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.