非模板类或非模板函数可以是类模板的友元。
#include <iostream> using namespace std; template <class Type> class Bar{ //普通非模版类 friend class FooBar; //普通非模版函数 friend void fun(); private: Type data; public: void setData(Type temp){ data = temp; } }; class FooBar{ public: //非模版类成员可以访问Bar类实例的任意成员 void print(){ Bar<int> bari; bari.setData(5); cout<<"data:"<<bari.data<<endl; Bar<string> bars; bars.setData("No"); cout<<"data:"<<bars.data<<endl; } }; //非模版函数可以访问Bar类实例的任意成员 void fun(){ Bar<int> bari; bari.setData(4); cout<<"data:"<<bari.data<<endl; Bar<string> bars; bars.setData("Yes"); cout<<"data:"<<bars.data<<endl; } int main(){ fun(); FooBar fooBar; fooBar.print(); return 0; }
2 一般模版友元关系
友元可以是类模版或者函数模版
#include <iostream> using namespace std; template <class Type> class Bar{ //模版类 template <class T> friend class FooBar; //模版函数 template <class T> friend void fun(const Bar<T>&); private: Type data; public: void setData(Type temp){ data = temp; } }; template <class T> class FooBar{ public: //模版类成员可以访问Bar类实例的任意成员 template <class U> void print(Bar<U> &bar){ cout<<"模版类:"<<bar.data<<endl; } }; //模版函数可以访问Bar类实例的任意成员 template <class T> void fun(const Bar<T> &bar){ cout<<"模版函数:"<<bar.data<<endl; } int main(){ Bar<int> bari; bari.setData(4); Bar<string> bars; bars.setData("Yes"); fun(bari); fun(bars); FooBar<int> fooBari; FooBar<string> fooBars; fooBari.print(bari); fooBars.print(bars); return 0; }
3 特定的模版友元
除了将一个模版的实例设为友元,类也可以只授予对特定实例的访问权。
#include <iostream> using namespace std; //模版声明 template <class T> class Bar; template <class T> class FooBar; template <class T> void fun(const Bar<T>&); template <class Type> class Bar{ //模版类特定实例 friend class FooBar<int>; //模版函数特定实例 friend void fun<int>(const Bar<int>&); private: Type data; public: void setData(Type temp){ data = temp; } }; template <class T> class FooBar{ public: template <class U> void print(Bar<U> &bar){ cout<<"模版类:"<<bar.data<<endl; } }; template <class T> void fun(const Bar<T> &bar){ cout<<"模版函数:"<<bar.data<<endl; } int main(){ Bar<int> bari; bari.setData(4); Bar<string> bars; bars.setData("Yes"); fun(bari); //fun(bars); error:Type data is private FooBar<int> fooBari; FooBar<string> fooBars; fooBari.print(bari); //fooBars.print(bars); error:Type data is private return 0; }