class complex
{
public:
double real,imag;
complex(double r=0.0,double i=0.0):real(r),imag(i){ };
complex operator-(const complex &c);
};
complex operator+(const complex &a,const complex &b)
{
return complex(a.real+b.real,a.imag+b.imag);//返回临时对象
}//重载为普通函数,参数个数 运算符目数
complex::operator-(const complex &c)
{
return complex(real-c.real+imag-c.imag);//返回临时对象
//重载为成员函数,参数个数为 运算符目数-1
} //real 为当前对象所指
C++:运算符重载
最新推荐文章于 2022-09-10 16:04:53 发布