在下面的示例中:
class A {
private: double content;
public:
A():content(0) {}
A operator+(const A& other) {
content += other.content;
return *this;
}
void operator=(const A& other) {
content = other.content;
}
};
A
是一个简单的双精度浮点数包装器,其中+
和=
运算符已被重载。在下面的使用中:
int main(int argc, char *argv[]) {
A a, b, c;
(a+b) = c ; // Why is this operation legal?
}
为什么(a+b) = c
可以编译通过?我想知道为什么这个语句是合法的,因为(a+b)
的结果必须是一个rvalue
。我没有从operator+
返回一个引用。
(a + b).display()
能正常工作,对吧? - Kerrek SB