在类模板中可以出现三种友元声明:
(1)普通非模板类或函数的友元声明,将友元关系授予明确指定的类或函数。
(2)类模板或函数模板的友元声明,授予对友元所有实例的访问权。
(3)只授予对类模板或函数模板的特定实例的访问权的友元声明。
要注意的是,友元函数并非成员函数,是改变了它对类成员的访问权限。
(1)没有什么好说的,如:
template<class T>
class A{
friend void fun();
//...
};
此例中fun可访问A任意类实例中的私有和保护成员
(2)
template<class T>
class A{
template<class T>
friend void fun(T u);
//...
};
这时友元使用与类不同的模板形参,T可以是任意合法标志符,友元函数可以访问A类的任何类实例的数据,即不论A的形参是int,double或其他都可以。
(3)
template<class T>
class A{
friend void fun<T>(T u);
//...
};
此时fun只有访问类中特定实例的数据。换句话说,此时具有相同模板实参的fun函数与A类才是友元关系。即假如调用fun时其模板实参为int,则它只具有A<int>的访问权限。当然friend void fun<T>(T u);中<>中的T可以是任意类型,比如int,double等
回到原问题,按(3)可改为:
template <class T> class List{
friend std::ostream& operator << <T>(std::ostream& os,const List<T>& slist);
//……
};
按(2)可改为:
template <class T> class List{
template <class T>
friend std::ostream& operator << (std::ostream& os,const List<T>& slist);
//……
};
在这里其实两者实现的最终效果一样的,因为调用输出运算符时需要访问的类实例的对象是它本身,所以形参T在第一种改法中一定匹配。
对类建立友元函数很容易。但是迁移到模板上却容易出现让人摸不着头脑的连接错误。
层次不够,不做分析,单纯介绍两种为类模板定义友元函数的方法
1 封闭型
![C++中模板类使用友元模板函数 C++中模板类使用友元模板函数](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![C++中模板类使用友元模板函数 C++中模板类使用友元模板函数](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![C++中模板类使用友元模板函数 C++中模板类使用友元模板函数](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![C++中模板类使用友元模板函数 C++中模板类使用友元模板函数](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![C++中模板类使用友元模板函数 C++中模板类使用友元模板函数](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![C++中模板类使用友元模板函数 C++中模板类使用友元模板函数](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![C++中模板类使用友元模板函数 C++中模板类使用友元模板函数](http://www.cppblog.com/Images/dot.gif)
![C++中模板类使用友元模板函数 C++中模板类使用友元模板函数](http://www.cppblog.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![C++中模板类使用友元模板函数 C++中模板类使用友元模板函数](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
2 开放型
![C++中模板类使用友元模板函数 C++中模板类使用友元模板函数](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![C++中模板类使用友元模板函数 C++中模板类使用友元模板函数](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![C++中模板类使用友元模板函数 C++中模板类使用友元模板函数](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![C++中模板类使用友元模板函数 C++中模板类使用友元模板函数](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![C++中模板类使用友元模板函数 C++中模板类使用友元模板函数](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![C++中模板类使用友元模板函数 C++中模板类使用友元模板函数](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![C++中模板类使用友元模板函数 C++中模板类使用友元模板函数](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![C++中模板类使用友元模板函数 C++中模板类使用友元模板函数](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![C++中模板类使用友元模板函数 C++中模板类使用友元模板函数](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![C++中模板类使用友元模板函数 C++中模板类使用友元模板函数](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![C++中模板类使用友元模板函数 C++中模板类使用友元模板函数](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![C++中模板类使用友元模板函数 C++中模板类使用友元模板函数](http://www.cppblog.com/Images/dot.gif)
![C++中模板类使用友元模板函数 C++中模板类使用友元模板函数](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
3 告诉编译器声明的设个是模板
![C++中模板类使用友元模板函数 C++中模板类使用友元模板函数](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![C++中模板类使用友元模板函数 C++中模板类使用友元模板函数](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![C++中模板类使用友元模板函数 C++中模板类使用友元模板函数](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![C++中模板类使用友元模板函数 C++中模板类使用友元模板函数](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![C++中模板类使用友元模板函数 C++中模板类使用友元模板函数](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![C++中模板类使用友元模板函数 C++中模板类使用友元模板函数](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![C++中模板类使用友元模板函数 C++中模板类使用友元模板函数](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![C++中模板类使用友元模板函数 C++中模板类使用友元模板函数](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![C++中模板类使用友元模板函数 C++中模板类使用友元模板函数](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![C++中模板类使用友元模板函数 C++中模板类使用友元模板函数](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![C++中模板类使用友元模板函数 C++中模板类使用友元模板函数](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![C++中模板类使用友元模板函数 C++中模板类使用友元模板函数](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![C++中模板类使用友元模板函数 C++中模板类使用友元模板函数](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![C++中模板类使用友元模板函数 C++中模板类使用友元模板函数](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![C++中模板类使用友元模板函数 C++中模板类使用友元模板函数](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![C++中模板类使用友元模板函数 C++中模板类使用友元模板函数](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![C++中模板类使用友元模板函数 C++中模板类使用友元模板函数](http://www.cppblog.com/Images/OutliningIndicators/None.gif)
![C++中模板类使用友元模板函数 C++中模板类使用友元模板函数](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![C++中模板类使用友元模板函数 C++中模板类使用友元模板函数](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![C++中模板类使用友元模板函数 C++中模板类使用友元模板函数](http://www.cppblog.com/Images/OutliningIndicators/InBlock.gif)
![C++中模板类使用友元模板函数 C++中模板类使用友元模板函数](http://www.cppblog.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
(这个方法在VS2010没通过)
建议:
如果希望使用函数与模板特化的类型相对应,则使用方法3(模板显示声明)
如果希望使用函数与模板特化的类型相独立,则使用方法2(二重模板)
简短的内联函数使用方法1
转载于:https://blog.51cto.com/7209253/1201974