考虑下面崩溃的简单代码:
#include <iostream>
struct test
{
int *x;
test(){ x = new int;}
~test(){delete x;}
};
int main() {
test x;
test y = std::move(x);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我的问题是,当对象的资源被移动时std::move,当它的析构函数被调用为超出范围的自然对象时会发生什么?这是否意味着我们不应该std::move在堆栈上调用对象?
谁负责释放std :: move移动的资源?
该对象的作者负责遵守五法则.
由于test需要自定义删除程序,因此也假定需要自定义移动和复制操作.
或者,int *x;可以使用智能指针替换该成员,从而无需使用自定义析构函数并遵守Zero of Zero.