私は怒鳴るconstメンバ、のconstポインタと列挙型クラスのメンバを持つクラス、C++コンストラクタと定数メンバポインタまたは定数メンバの移動、メモリリークを避けるためにそれらをヌル化する方法は?
サンプルコードのための私の質問があります。
`で「その他」の列挙型クラスのメンバーをnuliffyする方法- を移動先のコンストラクタ
- 移動コンストラクタで "その他"のconstポインタを無効にする方法 他のデストラクタは、 が構築されているオブジェクトのメモリを削除しないようにします。ポインターが有効になるように?
- 移動コンストラクタで "other"の定数メンバーを無効にする方法 他のデストラクタが呼び出されないようにするには?
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;
};
特別なケースとして0を使用して、enumを1から始めることができますか? – PlasmaHH
@ PlasmaHHどのように役立つだろうか?私は20分も費やしていますが、私のポストでこのコードタグを解決することはできません。ただ機能しません。 – codekiddy
値を変更したい場合は、単にconstにすることはできません。それは簡単です。 –