C++爱好者 - 网友留言

1432阅读 0评论2008-12-17 3JTwF2T
分类:

#include
class complex
{
public:
complex(int a=0,int b=0):t1(a),t2(b)
{}
complex operator+(complex va)
{t1+=va.t1;t2+=va.t2;return *this;}
friend ostream& operator<<(ostream&,complex);
private:
int t1;int t2;
};
ostream&  operator<<(ostream& ou,complex b) //对<<的重载函数的参数 ostream必须是引用形式
{
ou< return ou;
}

void main()
{
complex a(1,3),b(4,6);
a=a+b;
cout<}
我发现对<<的重载函数时,参数类型ostream必须是引用形式,不然就会出现ostream类中试图调用私有成员的错误,我看过源码发现ostream类中其拷贝构造函数被定义为protect(相当于私有,怪不得不能随便拷贝ostream对象),为什么要把ostream类中拷贝构造函数被定义为protect???这样定义,其拷贝构造函数还能发挥什么作用??

--------------------next---------------------

上一篇:C++爱好者 - 网友留言
下一篇:C++爱好者 - 网友留言