运算符重载为友元函数
运算符重载为类的友元函数,就必须把操作数全部通过形参的方式传递给运算符重载函数。
如:
class complex
{
public:
complex(double x,double y):real(x),imag(y) {}
//friend complex operator + (complex c1,complex c2); //友元函数
//friend complex operator - (complex c1,complex c2);
complex operator +(complex c2) //非友元函数定义
{
return complex(real + c2.real,imag + c2.imag);
}
void display():
private:
double real;
double imag;
};