#include<iostream> using namespace std; class A{ public: A()=default; A(int&&a){moves(a);} void moves(int &a) { t=a; cout<<t<<endl; } private: int t; }; int main() { A(33); }
如代码所示,A构造函数内,原右值a自动变成左值(不是通过拷贝构造函数或者移动构造函数),因此moves只能是接受左值,不能接受右值,因为a变成左值了
#include<iostream> using namespace std; class A{ public: A()=default; A(int&&a){moves(a);} void moves(int &a) { t=a; cout<<t<<endl; } private: int t; }; int main() { A(33); }