我看过很多教程,并试图在 stackoverflow 上找到答案,但没有成功。
我不确定的是;在重载运算符时,是否有一些实践何时按值或按引用返回?
例如
Class &operator+(){
Class obj;
//...
return obj;
}
Run Code Online (Sandbox Code Playgroud)
或相同的东西但按价值
Class operator+(){
Class obj;
//...
return obj;
}
Run Code Online (Sandbox Code Playgroud)
我想提一下,我注意到在几乎 90% 的情况下,当返回相同的对象 ( *this) 时,会在返回的同一对象上引用。有人也可以解释为什么会这样吗?
c++ operator-overloading return-by-reference c++11 return-by-value