我有一个类,它有一个常量成员、常量指针和枚举类成员,
我对以下示例代码的问题:
- 如何在移动构造函数中正确地取消“其他”的枚举类成员(分配什么值?)
- 如何在移动构造函数中使“其他”的常量指针无效,以便其他的析构函数不会删除正在构造的对象的内存,并且指针仍然有效?
- 如何在移动构造函数中使“其他”的常量成员无效,以免调用其他的析构函数?
enum class EnumClass
{
VALUE0, // this is zero
VALUE1
};
class MyClass
{
public:
MyClass() :
member(EnumClass::VALUE1),
x(10.f),
y(new int(4)) { }
MyClass(MyClass&& other) :
member(other.member),
x(other.x),
y(other.y)
{
// Can I be sure that this approach will nullify a "member" and avoid
// destructor call of other
other.member = EnumClass::VALUE0;
// Or shall I use this method?
other.member = static_cast<EnumClass>(0);
// ERROR how do I nullify "x" to avoid destructor call of other?
other.x = 0.f;
// ERROR the same here, delete is going to be called twice!
other.y = nullptr;
}
~MyClass()
{
delete y;
}
private:
EnumClass member;
const float x;
int* const y;
};