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.
(1) ordinary friend £ º
Template<class t>
Class a{
friend void Fun ();
//...
};
In this example, fun can access private and protected members in an arbitrary class instance
(2) General template friend relationship
Template<class type>
Class a{
Template<classt>
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) A specific template friend relationship
Template <class t> void Fun (T,U);//For a friend relationship with a specific template, the declaration here is required
Template<class t>
Class a{
Friend voidfun<t> (T u);//This can also be friend void fun<char> (char 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 friendvoid fun<t> (Tu); T in <> can be any type, such as Int,double
It is easy to build a friend function on a class. But migrating to templates is prone to confusing connection errors.
The level is not enough, do not do analysis, simply introduce two methods to define the friend function for the class template
1 sealed
template< TypeName T >
Class MyClass
{
friend void function (myclass< T > &arg)
{
}
}; Important: The friend function is defined in the template body.
2 Open Type
template< TypeName T >
Class MyClass
{
template< TypeName C >
friend void function (myclass< C > &arg);
};
template< TypeName C >
void function (myclass< C > &arg)
{
Important: Templates are created in the template body.
3 tells the compiler to declare that the set is a template
#include <iostream>
using namespace Std;
Template < typename T >
Class A
{
Friend Ostream &operator<< < T > (ostream &, const a< T > &);
};
Template < typename T >
Ostream &operator<< (ostream &output, const a< T > &a)
{
Output << "Reload success" << Endl;
return output;
}
int main ()
{
a< int > A;
cout << A;
Important: Display the template declaration < T> after the overloaded operator or function, and 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
Reference http://blog.sina.com.cn/s/blog_7c2c21230100svc3.html
Template classes in C + + use friend template functions