Template classes in C + + use friend template functions

Source: Internet
Author: User

There are three friend declarations that can appear in a class template:
(1) A friend declaration of a generic non-template class or function that grants a friend relationship to a class or function that is explicitly specified.
(2) A friend declaration of a class template or function template that grants access to all instances of a friend.
(3) A friend declaration that grants access only to a specific instance of a class template or function template.

It is important to note that the friend function is not a member function, but it changes its access to the members of the class.

(1) There is nothing to say, such as:

Template<class t>

Class a{

friend void Fun ();

//...

};
In this example, fun can access private and protected members in an arbitrary class instance

(2)

Template<class t>

Class a{

Template<class t>

friend void Fun (T u);

//...

};

When a friend uses a template parameter that differs from the class, T can be any valid identifier, and the friend function can access the data of any class instance of Class A, that is, whether the formal parameter of a is int,double or otherwise.

(3)

Template<class t>

Class a{

friend void fun<t> (T u);

//...

};

At this time, fun only accesses data for a specific instance of the class. In other words, a fun function with the same template arguments at this point is a friend relationship with Class A. That is, if you call fun when its template argument is int, it only has access to a<int>. Of course friend void fun<t> (t u); t in <> can be any type, such as Int,double

Back to the original question, press (3) to change to:
Template <class t> class list{
Friend std::ostream& operator << <T> (std::ostream& os,const list<t>& slist);
......
};

Press (2) to change to:

Template <class t> class list{

Template <class t>
Friend std::ostream& operator << (std::ostream& os,const list<t>& slist);
......
};
The final effect of the two implementations is the same here, because the object of the class instance that needs to be accessed when the output operator is called is itself, so the formal parameter T must match in the first method of modification.


  

It is easy to build a friend function on a class. But migrating to templates is prone to confusing connection errors.
Level is not enough, do not do analysis, simply introduce two methods for class template to define the friend function.

1 sealed

template< TypeName T >classvoid function (myclass< t > &arg) {           ...   }};

2. Open type

template<class t>class  a{  Template<class t>     void fun (T u); // ... };

When a friend uses a template parameter that differs from the class, T can be any valid identifier, and the friend function can access the data of any class instance of Class A, that is, whether the formal parameter of a is int,double or otherwise.

3, tell the compiler to declare a template

template<class t>class  a{   void fun<t>(T u); // ... };

At this time, fun only accesses data for a specific instance of the class. In other words, a fun function with the same template arguments at this point is a friend relationship with Class A. That is, if you call fun when its template argument is int, it only has access to a<int>. Of course friend void fun<t> (t u); t in <> can be any type, such as Int,double

Important: Display the following overloaded operator or function with template declaration < T;, tell the compiler that the friend function is a consistent type template.

Suggestions:
Use Method 3 (Template display declaration) if you want to use functions that correspond to types of template specificity
If you want to use a function that is independent of the type of template specificity, use Method 2 (double template)
Short inline function using Method 1

Template classes in C + + use friend template functions

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.