#include <iostream> using namespace std; /*友元模板函数在类的内部声明,可以实例化多次,因为每次实例化都会生成相应的版本*/ template<class Type> class Bar { template<class T> friend class Foo; friend ostream& operator << (ostream& os,const Bar<Type> &b) {return os<<b.pri;} Type pri; }; template<class T> class Foo { public: template<class U> void fcn(const Bar<U>& b); }; template<class T> template<class U> void Foo<T>:: fcn(const Bar<U>& b) { cout<<b.pri<<endl; } /*Foo类的任何类型可以访问Bar类的任何类型*/ int main(void) { Bar<float> bf; Bar<int> bi; Foo<int> fo; cout<<bf<<endl; cout<<bi<<endl; fo.fcn(bi); fo.fcn(bf); system("pause"); return 0; } #include <iostream> using namespace std; template<class Type> class Bar { template<class T> friend class Foo; template<class T> friend ostream& operator << (ostream& os,const Bar<T> &b); Type pri; }; template<class T> ostream& operator << (ostream& os,const Bar<T> &b) { return os<<b.pri; } template<class T> class Foo { public: template<class U> void fcn(const Bar<U>& b); }; template<class T> template<class U> void Foo<T>:: fcn(const Bar<U>& b) { cout<<b.pri<<endl; } /*Foo类的任何类型可以访问Bar类的任何类型*/ int main(void) { Bar<float> bf; Bar<int> bi; Foo<int> fo; cout<<bf<<endl; cout<<bi<<endl; fo.fcn(bi); fo.fcn(bf); system("pause"); return 0; } #include <iostream> using namespace std; /*特定的模板友元关系*/ template <typename T> class Bar; template<class T> class Foo; template<class U> ostream& operator << (ostream& os,const Bar<U> &b); template<class Type> class Bar { friend class Foo<char*>; friend ostream& operator << <>(ostream& os,const Bar<char*> &b); Type pri; }; template<class T> ostream& operator << (ostream& os,const Bar<T> &b) { return os<<b.pri; } /*ostream& operator << (ostream& os,const Bar<char*> &b) { return os<<b.pri; }*/ template<class T> class Foo { public: template<class U> void fcn(const Bar<U>& b); }; template<class T> template<class U> void Foo<T>:: fcn(const Bar<U>& b) { cout<<b.pri<<endl; } /*只有Foo<char*>可以访问Bar类的任何类型*/ int main(void) { Bar<float> bf; Bar<int> bi; Foo<int> fo; Foo<char*> fc; Bar<char*> bc; //cout<<bf<<endl; //cout<<bi<<endl; cout<<bc<<endl; //fo.fcn(bi); //fo.fcn(bc); fc.fcn(bi);//可以访问Bar的任意类型 fc.fcn(bf); system("pause"); return 0; } #include <iostream> using namespace std; /*特定的模板友元关系*/ template <typename T> class Bar; template<class T> class Foo; template<class U> ostream& operator << (ostream& os,const Bar<U> &b); template<class Type> class Bar { friend class Foo<char*>; /*定义在内部也可多次实例化*/ friend ostream& operator << (ostream& os,const Bar<char*> &b) {return os<<b.pri;} Type pri; }; template<class T> class Foo { public: template<class U> void fcn(const Bar<U>& b); }; template<class T> template<class U> void Foo<T>:: fcn(const Bar<U>& b) { cout<<b.pri<<endl; } /*只有Foo<char*>可以访问Bar类的任何类型*/ int main(void) { Bar<float> bf; Bar<int> bi; Foo<int> fo; Foo<char*> fc; Bar<char*> bc; //cout<<bf<<endl; //cout<<bi<<endl; cout<<bc<<endl; //fo.fcn(bi); //fo.fcn(bc); fc.fcn(bi);//可以访问Bar的任意类型 fc.fcn(bf); system("pause"); return 0; } #include <iostream> using namespace std; /*特定的模板友元关系*/ template <typename T> class Bar; template<class T> class Foo; template<class U> ostream& operator << (ostream& os,const Bar<U> &b); template<class Type> class Bar { friend class Foo<Type>; friend ostream& operator << <>(ostream& os,const Bar<Type> &b);//加<>表示模板特化 Type pri; }; template<class T> ostream& operator << (ostream& os,const Bar<T> &b) { return os<<b.pri; } template<class T> class Foo { public: void fcn(const Bar<T>& b); }; template<class T> void Foo<T>:: fcn(const Bar<T>& b) { cout<<b.pri<<endl; } /*只有Foo<char*>可以访问Bar类的任何类型*/ int main(void) { Bar<float> bf; Bar<int> bi; Foo<int> fo; Foo<char*> fc; Bar<char*> bc; cout<<bf<<endl; cout<<bi<<endl; cout<<bc<<endl; //fo.fcn(bi); //fo.fcn(bc);//error类型不一致 fc.fcn(bc);//只能访问与Bar对应的类型 fo.fcn(bi); system("pause"); return 0; } #include <iostream> using namespace std; /*特定的模板友元关系*/ template <typename T> class Bar; template<class T> class Foo; template<class Type> class Bar { friend class Foo<char*>; friend ostream& operator << (ostream& os,const Bar<char*> &b); Type pri; }; ostream& operator << (ostream& os,const Bar<char*> &b) { return os<<b.pri; } template<class T> class Foo { public: template<class U> void fcn(const Bar<U>& b); }; template<class T> template<class U> void Foo<T>:: fcn(const Bar<U>& b) { cout<<b.pri<<endl; } /*只有Foo<char*>可以访问Bar类的任何类型*/ int main(void) { Bar<float> bf; Bar<int> bi; Foo<int> fo; Foo<char*> fc; Bar<char*> bc; //cout<<bf<<endl; //cout<<bi<<endl; cout<<bc<<endl; //fo.fcn(bi); //fo.fcn(bc); fc.fcn(bi);//可以访问Bar的任意类型 fc.fcn(bf); system("pause"); return 0; } #include <iostream> using namespace std; /*友元模板函数在类的内部声明,只能实例化一次,实例化两次会出现重复定义, 因为它实例化一次的版本可以访问任意类型的Bar*/ template<class Type> class Bar { template<class T> friend class Foo; template<class T> friend ostream& operator << (ostream& os,const Bar<T> &b) {return os<<b.pri;} Type pri; }; /*类的成员函数在类内部定义没影响*/ template<class T> class Foo { public: template<class U> void fcn(const Bar<U>& b) { cout<<b.pri<<endl; } }; void test_Bar() { //这里实例化operator << (ostream& os,const Bar<float> &b),适用于任何Bar版本 Bar<float> bf; //Bar<int> bi;//重复定义 operator << cout<<bf<<endl; //cout<<bi<<endl; } void test_Foo() { Bar<float> bf; //Bar<int> bi; Foo<int> fo; Foo<float> ff; //fo.fcn(bi); fo.fcn(bf); ff.fcn(bf); } int main(void) { test_Bar(); test_Foo(); system("pause"); return 0; } #include <iostream> using namespace std; template<class Type> class Bar { template<class T> friend class Foo; template<class T> friend ostream& operator << (ostream& os,const Bar<T> &b); Type pri; }; template<class T> ostream& operator << (ostream& os,const Bar<T> &b) { return os<<b.pri; } template<class T> class Foo { public: void fcn(const Bar<T>& b); }; template<class T> void Foo<T>:: fcn(const Bar<T>& b) { cout<<b.pri<<endl; } /*Foo类的任何类型只可以访问Bar类的对应类型*/ int main(void) { Bar<float> bf; Bar<int> bi; Foo<int> fo; cout<<bf<<endl; cout<<bi<<endl; fo.fcn(bi); //fo.fcn(bf);error system("pause"); return 0; }