重载<<时 模板类中私有成员无法直接访问而非模板类可以直接访问

helloair 2003-09-26 06:20:27
这个是非模板类的实现,在VC6下编译通过
#include <iostream>
using namespace std;

class ZTest;
ostream& operator << ( ostream& os,const ZTest& test);

class ZTest
{
friend ostream& operator << ( ostream& os,const ZTest& test);
public:
ZTest ():a(0),b(0) {}
void set (int i,int j) { a=i;b=j; };
private:
int a;
int b;
};

ostream& operator << ( ostream& os, const ZTest& test )
{
os <<"("<< test.a << " " << test.b << ")";
return os;
}

int main ()
{
ZTest ti;
ti.set(9,10);
cout << ti << endl;
return 0;
}

这是模板类的实现,编译报错
error C2248: 'a' : cannot access private member declared in class 'ZTest<int>'
error C2248: 'b' : cannot access private member declared in class 'ZTest<int>'

test.exe - 2 error(s), 0 warning(s)

#include <iostream>
using namespace std;

template <class T> class ZTest;
template <class T> ostream& operator << ( ostream& os,const ZTest<T>& test);

template <class T>
class ZTest
{
friend ostream& operator << ( ostream& os,const ZTest<T>& test);
public:
ZTest ():a(0),b(0) {}
void set (T i,T j) { a=i;b=j; };
private:
T a;
T b;
};

template <class T> ostream& operator << ( ostream& os, const ZTest<T>& test )
{
os <<"("<< test.a << " " << test.b << ")";
return os;
}

int main ()
{
ZTest<int> ti;
ti.set(9,10);
cout << ti << endl;
return 0;
}
...全文
72 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
招RD和QA 2004-02-18
  • 打赏
  • 举报
回复
mark
helloair 2003-09-27
  • 打赏
  • 举报
回复
在VC6中:对模板类来说,把友员函数的实现直接写在类内,这个函数就能被识别,如果实现在类外,就不能识别此友员函数,因此导致不能访问类的私有成员。

thanks to Wolf0403(完美废人) sevecol(sevecol.blogone.net) xueweizhong(薛卫忠)
xueweizhong 2003-09-26
  • 打赏
  • 举报
回复
VC6对模板的支持不是很好,但实现这个东东的人说,
只要你的函数实现直接写在类内,我们对模板的支持
就会变得很好.

遵循这个原则,把友员函数的实现直接写在类内,VC6.0
就可以正常处理了:

template <class T>
class ZTest
{
friend ostream& operator << ( ostream& os,const ZTest<T>& test)
{
os <<"("<< test.a << " " << test.b << ")";
return os;
}
...
};

// 把原来这里的实现移到类内.


to ...

template <class T>
class ZTest
{
template <class T> // 就是少这个。
friend ostream& operator << ( ostream& os,const ZTest<T>& test);
// rename it as output
...
这个实现带有危险性:
本来只有 operator <<(..., ZTest<int>...)是ZTest<int>的友员,
而现在这样修改后,

operator <<(..., ZTest<int>...)是ZTest<int>的友员,
operator <<(..., ZTest<char>...)是ZTest<int>的友员,
operator <<(..., ZTest<float>...)是ZTest<int>的友员,
...
这无穷多个操作函数都变得可以访问ZTest<int>的私有成员了.
Wolf0403 2003-09-26
  • 打赏
  • 举报
回复
template <class T>
class ZTest
{
template <class T> // 就是少这个。
friend ostream& operator << ( ostream& os,const ZTest<T>& test);
public:
ZTest ():a(0),b(0) {}
void set (T i,T j) { a=i;b=j; };
private:
T a;
T b;
};

template <class T>
ostream& operator << ( ostream& os, const ZTest<T>& test )
{
os <<"("<< test.a << " " << test.b << ")";
return os;
}
以上编译通过(VC71)。
sevecol 2003-09-26
  • 打赏
  • 举报
回复
这样改:

#include <iostream>
using namespace std;

template <class T>
class ZTest;

template <class T>
ostream& operator << ( ostream& os, const ZTest<T>& test )
{
os <<"("<< test.a << " " << test.b << ")";
return os;
}

template <class T>
class ZTest
{

template<typename N>
friend ostream& operator << ( ostream& os,const ZTest<T>& test);
public:
ZTest ():a(0),b(0) {}
void set (T i,T j) { a=i;b=j; };
private:
T a;
T b;
};

24,855

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧