相关疑难解决方法(0)

分配给rvalue:为什么要编译?

在以下示例中:

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;
  }

};
Run Code Online (Sandbox Code Playgroud)

A是一个double的简单包装器,+并且=运算符已经过载.在以下用途中:

 int main(int argc, char *argv[]) {
    A a, b, c;
    (a+b) = c ; // Why is this operation legal?
}
Run Code Online (Sandbox Code Playgroud)

为什么(a+b) = c编译?我想知道为什么这个陈述是合法的,因为结果(a+b)必须是一个rvalue.我没有从中返回参考operator+.

c++ reference rvalue lvalue

5
推荐指数
1
解决办法
656
查看次数

标签 统计

c++ ×1

lvalue ×1

reference ×1

rvalue ×1