Complex operator +( Complex &, Complex &) 与Complex operator +( Complex , Complex )区别和联系#includeclass Complex{public:Complex(){real=0;imag=0;}Complex( double r){real=r;imag=0;
来源:学生作业帮助网 编辑:作业帮 时间:2024/11/28 12:16:34
Complex operator +( Complex &, Complex &) 与Complex operator +( Complex , Complex )区别和联系#includeclass Complex{public:Complex(){real=0;imag=0;}Complex( double r){real=r;imag=0;
Complex operator +( Complex &, Complex &) 与Complex operator +( Complex , Complex )区别和联系
#include
class Complex{
public:
Complex()
{
real=0;
imag=0;
}
Complex( double r)
{
real=r;
imag=0;
}
Complex(double r, double i):real(r),imag(i) {}
friend Complex operator + ( Complex & , Complex & );
void display();
private:
double real;
double imag;
};
Complex operator +( Complex &k, Complex &t) //这里用这个不对,但用Complex operator +( Complex //k, Complex t) 是对的.或者用Complex operator +( //const Complex &k, const Complex &t) 也是对的(下// 面///函数定义一起修改就行).说说这三种区别和联系
{
return Complex(k.real+t.real, k.imag+t.imag);
}
void Complex::display()
{
cout
Complex operator +( Complex &, Complex &) 与Complex operator +( Complex , Complex )区别和联系#includeclass Complex{public:Complex(){real=0;imag=0;}Complex( double r){real=r;imag=0;
问题在于这句:
c3=c2+6.5;
6.5被自动转成临时对象,而临时对象是const类型的,可以参数这个文章 :