I have a class that has a const member, const pointer and enum class member,
My Questions for the sample code bellow:
- How to nuliffy a enum class member of "other"` in move constructor properly (what value to assign to it?)
- How to nullify a const pointer of "other"` in move constructor so that destructor of other does not delete a memory of object that is being constructed, and so that a pointer reamins valid?
- How to nullify a constant member of "other" in move constructor so that destructor of other does not get called?
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;
};