#include <iostream>
using std::cout;
using std::endl;
class MoveTest {
public:
MoveTest(int i) :
_i(i) {
cout << "Normal Constructor" << endl;
}
MoveTest(const MoveTest& other) :
_i(other._i) {
cout << "Copy Constructor" << endl;
} // = delete;
MoveTest& operator=(const MoveTest& other) {
return *this;
} //= delete;
MoveTest(MoveTest&& o) {
_i = o._i;
cout << "Move Constructor" << endl;
}
MoveTest& operator=(const MoveTest&& o) {
cout << "Move Assign" << endl;
return *this;
}
//private:
int _i;
};
MoveTest get() {
MoveTest t = MoveTest(2);
cout << "get() construct" << endl;
return t; //MoveTest(1);
}
int main(int argc, char **argv) {
MoveTest t(get());
cout << t._i << endl;
t = get();
}
有结果:
常规构造函数
get()构造
2
常规构造函数
get()构造
移动分配
在主函数中,“ MoveTest t(get());”既不使用复制结构也不使用移动结构。这就是为什么?