向大家请教一个有关C++的很简单的问题。
plizi 2001-09-16 10:08:56 #include <iostream.h>
class D
{
int x,y,z;
public:
D operator = (D t);
friend D operator ++(D &op,int); // postfix
friend D operator ++(D &op); // prefix
void show();
D(int xx=0,int yy=0,int zz=0) // constructor
{x=xx;y=yy;z=zz;}
};
D D::operator=(D t) // 重载 =
{
x=t.x; y=t.y; z=t.z;
return *this;
}
D operator++(D &op,int) // 重载 object++
{
// 要实现 obj1 = obj2++ (把obj2的
// 各成员的值赋给obj1之后,然后再自增)?
// 上面的代码不能完成这个要求。
}
D operator++(D &op) // 重载 ++object
{
++op.x; ++op.y; ++op.z;
return op;
}
void D::show()
{
cout<<x<<","<<y<<","<<z<<"\n";
}
void main(void)
{
D const t;
D a,b,c;
cout<<"a: "; a.show(); // a: 0,0,0
cout<<"b: "; b.show(); // b: 0,0,0
cout<<"c: "; c.show(); // c: 0,0,0
cout<<"a=++c: "; a=++c;
a.show(); // a: 1,1,1
c=t;
cout<<"b=c++: "; b=c++;
b.show(); // b: 1,1,1
// 如何让b等于0,0,0 ?
}