作者:靜trevis_263 | 来源:互联网 | 2023-05-19 07:37
class Arr
{
public:
Arr() { cout <<"constructor Arr" < ~Arr() { cout <<"distructor Arr" < Arr(const Arr& arr) { cout <<"拷贝构造" < Arr(Arr&& arr) { cout <<"移动构造" <};
int main(int argc, char* argv[])
{
system("chcp 65001");
const Arr arr1;
Arr arr2{ std::move(arr1) };
Arr arr3;
Arr arr4{ std::move(arr3) };
}
第一,不要在你希望能移动对象的时候,声明他们为const
。对const
对象的移动请求会悄无声息的被转化为拷贝操作。第二点,std::move
不仅不移动任何东西,而且它也不保证它执行转换的对象可以被移动。关于std::move
,你能确保的唯一一件事就是将它应用到一个对象上,你能够得到一个右值。