Membership statement in the class template

Source: Internet
Author: User

There are three types of membership declarations that can appear in the class template:

1. Non-template friends or friends Functions

The Foo () member functions bar () and foobar classes are the friends of all instances of the queueitem class template.

Class Foo {
Void bar ();
};
 
Template <class T>
Class queueitem {
Friend class foobar;
Friend void Foo ();
Friend void FOO: bar ();
//...
};

2. bound user meta template or function template:

A one-to-one ing is defined between the instance of the queueitem class template and its friends. For each type of queueitem instance, foobar Foo () and queue <t> :: A single related instance of Bar () is youyuan.

Template <class type>
Class foobar {...};
 
Template <class type>
Void Foo (queueitem <type> );
 
Template <class type>
Class queue {
Void bar ();
 
//...
};
 
Template <class type>
Class queueitem {
Friend class foobar <type>;
Friend void Foo <type> (queueitem <type> );
Friend void queue <type >:: bar ();
 
//...

};

Friend void Foo <type> (queueitem <type> );

The function name is followed by the explicit template real parameter table Foo <type>. This syntax can be used to specify the instance of the function template Foo () referenced by the membership declaration.

If the explicit template parameters are omitted, the following is shown:

Friend void Foo (queueitem <type> );
The youyuan declaration is interpreted as referencing a non-template function, and the parameter type of this function is an instance of the queueitem class template.

3 unbound user meta template or function template not bound

A one-to-many ing is defined between the instance of the queueitem class template and its friends,

All foobar Foo () and queue <t >:: bar () Instances of each type of queueitem are friends, as shown below:

Template <class type>
Class queueitem {
Template <class T>
Friend class foobar;
 
Template <class T>
Friend void Foo (queueitem <t> );
 
Template <class T>
Friend void queue <t >:: bar ();
 
//...

};

We should note that the compiler before the Standard C ++ does not support this kind of membership statement in the class template.

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.