c++中重载问题,为何"+"不能连续用?
问题描述:
随便一个简单代码:
#include <iostream>
#include <string>
using namespace std;
class Complex
{
double real,imag;
public:
Complex(double r,double i):real(r),imag(i){}
void display(const string &name)
{cout << name << real << "," << imag << "i" << endl;}
friend Complex& operator+(Complex &c1,Complex &c2);
};
Complex& operator+(Complex &c1,Complex &c2)
{
Complex c;
c.real = c1.real + c2.real;
c.imag = c1.imag + c2.imag;
return c;
}
int main()
{
Complex c1(1,1),c2(2,2),c3;
c3 = c1 + c2;
c3 = c3 + c1; //为何这两行不能写成c3=c1+c2+c1?
c3.display("c3 = ");
system("pause"); return 0;
}
答
改后代码如下
#include <iostream>
#include <string>
using namespace std;
class Complex
{
private:
double real,imag;
public:
Complex() = default;
Complex(double r,double i):real(r),imag(i){}
void display(const string &name)
{cout << name << real << "," << imag << "i" << endl;}
Complex& operator+=(const Complex &c2);
};
Complex& Complex::operator+=(const Complex &c2)
{
real += c2.real;
imag += c2.imag;
return *this;
}
Complex operator+(const Complex &c1, const Complex& c2)
{
Complex c = c1;
c += c2;
return c;
}
int main()
{
Complex c1(1,1),c2(2,2),c3;
// c3 = c1 + c2;
// c3 = c3 + c1; //为何这两行不能写成c3=c1+c2+c1?
c3 = c1 + c2 + c1;
c3.display("c3 = ");
return 0;
}
附上一段《C++ Primer》的一个准则有助于我们在将运算符定义为成员函数还是普通的非成员函数做出却则:
- 赋值(=),下标([]),调用(())和成员访问箭头(->)运算符必须是成员、
- 符合赋值运算符一般来说应该是成员,但并非必须,这一点与赋值运算符略有不同。
- 改变对象状态的运算符符或者与给定类型密切相关的运算符,如递增,递减和解引用运算符,通常应该是成员。
- 具有对称性的运算符可能转换任意一端的运算对象,例如算术,相等性,关系和位运算符等,因此他们通常应该是普通的非成员函数。
题外话:
一个留待你自己去解决的问题:为什么调用operator+=来定义operator+比其他方法更有效?
答
因为你重载的函数格式就只是两个复数相加呀,如果有三个复数相加,程序肯定报错。再把书上概念好好看看吧。
答
首先来说这个操作是可以的,但是要改动一下
#include <iostream>
#include <string>
using namespace std;
class Complex
{
private:
double real,imag;
public:
Complex() = default;
Complex(double r,double i):real(r),imag(i){}
void display(const string &name)
{cout << name << real << "," << imag << "i" << endl;}
Complex operator+(Complex &c1);
};
Complex Complex::operator+(Complex &c2)
{
Complex c;
c.real = real + c2.real; // c.real = this->real + c2.real;
c.imag = imag + c2.imag; // c.imag = this->imag + c2.imag;
return c;
}
int main()
{
Complex c1(1,1),c2(2,2),c3;
// c3 = c1 + c2;
// c3 = c3 + c1; //为何这两行不能写成c3=c1+c2+c1?
c3 = c1 + c2 + c1;
c3.display("c3 = ");
return 0;
}
一般来说operator+重载不需要声明为友元,而且也不返回引用
在原始代码里,如果返回引用会有报错!!!即使它通过了编译。
在函数作用域中,c是存在的,但是结束后,c不存在了,你引用的c跑哪去了?