In Effective C++, Item 3, Scott Meyers suggests overloading operator*
for a class named Rational
:
class Rational { ... };
const Rational operator*(const Rational& lhs, const Rational& rhs);
收益价值为<代码>const-standard”的原因在以下各行文中作了解释:如果未填写const
,方案人员可撰写如下的代码:
(a * b) = c;
或更可能的话:
if (a*b = c)
Fair enough. Now I’m confused as I thought that the return value of a function, here operator*, was a rvalue, therefore not assignable. I take it not being assignable because if I had:
int foo();
foo() += 3;
that would fail to compile with invalid lvalue in assignment
.
Why doesn’t that happen here? Can someone shed some light on this?
www.un.org/Depts/DGACM/index_spanish.htm 我在斯科特·迈耶斯的这一项目上看到了许多其他线索,但没有人解决我在这里暴露的珍贵问题。