类型:转载 责任编辑:asp.net 日期:2007/05/23
热门软件下载:
如题
例如
template<class Type>
class A{
....
};
class B{
....
};
请问怎样在B中声明A为友元?
网友回答:
template<class Type>
friend class A;
#include<iostream>
template<class Type>
class B;
class A{
private:
int i;
int j;
template<class Type> friend class B; //怎样声明一个模板类为非模板类的友元
public:
A(int ii=1,int jj=1):i(ii),j(jj)
{}
};
template<class Type>
class B{
private:
Type clb;
int bb;
public:
B(A a,Type val)
{
clb=val;
bb=a.i+a.j;
}
void display()
{
std::cout<<clb<<" "<<bb<<std::endl;
}
};
int main()
{
A a(3,5);
B<char> b(a,A);
b.display();
system("Pause");
return 0;
}