CC::onError
が以下の例でconstである必要がないのはなぜですか? BB:onError
はconst以外のコンパイラエラーでなければなりません。派生クラスはいつメソッドをconstとして実装する必要がありますか?
BBはonErrorをconst elseコンパイラエラーとして実装する必要があります。
CCは非constとして実装できます。どうして?
class AA {
public:
AA() {};
virtual ~AA() {};
// On any error
virtual void onError(int32_t error) const = 0;
};
class BB : public AA {
public:
BB() {};
virtual ~BB() {};
virtual void onError(int32_t error) const {
//changing x is invalid here due to const
//method required to be const else compiler error
}
protected:
int x;
};
class CC : public BB {
public:
CC() {};
virtual ~CC() {};
virtual void onError(int32_t error) {
x = 5;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
CC c;
return 0;
}
@Thedizようこそ。将来の読者が探している情報をすばやく見つけられるように、この質問や別の質問を受け入れることを忘れないでください。すべての参加者がそうする限り、SOは貴重なツールです。 ;-) – skypjack