【发布时间】:2015-11-18 15:29:08
【问题描述】:
在以下示例中:
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 是一个 double 的简单包装器,其中 + 和 = 运算符已被重载。在以下用途中:
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()有效,对吧?
标签: c++ reference lvalue rvalue