ここでミュータブルの使用が適切かどうかお尋ねしたいと思います。
#include <iostream>
class Base
{
protected:
int x;
public:
virtual void NoMod() const
{
std::cout << x << std::endl;
}
void Draw() const
{
this->NoMod();
}
};
class Derive : public Base
{
private:
mutable int y;
public:
void NoMod() const
{
y = 5;
}
};
int main()
{
Derive derive;
// Test virtual with derive
derive.Draw();
return 0;
}
Base クラスはサードパーティのライブラリです。私はそれを拡張して、独自の NoMod() を提供しています。ライブラリ独自の NoMod() を const として宣言しています。
私の NoMod() は、独自のメンバー変数を変更する必要があるという点で Base とは異なります。
したがって、自分の NoMod() をコンパイルして、Draw() が呼び出されたときに呼び出されるようにするには、
1) Derive::NoMod() を const として実装し
ます。 2) int y を変更可能にします。
これは私ができる最善のことですか?