"="作为运算符重载,必须是成员函数


源程序:

#include
using namespace std;

class myComplex
{
private:
double real,imag;
public:
myComplex();
myComplex(double r,double i);
myComplex &operator=(const myComplex &c);
void outCom();
};

myComplex::myComplex()
{
real=0;
imag=0;
}

myComplex::myComplex(double r,double i)
{
real=r;
imag=i;
}

void myComplex::outCom()
{
cout<<"("<}

myComplex &myComplex::operator=(const myComplex &c1)
{
this->real=c1.real;
this->imag=c1.imag;
return *this;
}

int main()
{
myComplex c1(1,2),c2(3,4),res;
c1.outCom();
c2.outCom();
res=c1=c2;
res.outCom();
return 1;
}

C++