场景1:
move(obj)函数的功能是把obj当做右值处理,可以应用在对象的移动上。
注意,如果仅仅是定义右值引用,那么obj本身不会被移走,在作为参数时会发生obj被移走:
string str = "test";
string&& r = move(str);
cout<< r <<endl;
cout<< str <<endl;
string t(r);
cout<< t <<endl;
cout<< str <<endl;
system("pause");
输出结果:
如果在参数中使用move()包裹:
string str = "test";
string&& r = move(str);
cout<< r <<endl;
cout<< str <<endl;
string t(move(r));
cout<< t <<endl;
cout<< str <<endl;
system("pause");