【发布时间】:2012-11-28 18:25:01
【问题描述】:
我已经用 vs2011 编译了这段代码。它首先打印 constructor,然后打印 copy constructor。 但是,如果我将函数更改为返回 a 而不是 ap,它将移动对象。这是一个错误还是为什么会这样? *ap 不是右值吗?
struct A
{
A() { cout << "constructor" << endl;}
A(const A&) { cout << "copy constructor " << endl;}
void operator=(const A&) { cout << "assignment operator" << endl; }
A( A&&) { cout << "move copy constructor" << endl;}
void operator=(A&&) { cout << "move assignment operator" << endl;}
};
A func() { A a; A *ap = &a; return *ap; }
int main()
{
A a = func();
return 0;
}
【问题讨论】:
标签: c++ c++11 move-semantics rvalue