私はこの下のような何かを達成したいと思います:派生クラスのオブジェクトをリセット
class A {
public:
virtual void reset() {
// 1). if there's no override to this function,
// then whatever derived from A should get reset
// to its constructed state, e.g. if B derives from
// A, then *this = B();
// 2). if there is an override to reset in the derived
// class, call the reset in the derived class
}
};
class B: public A {
public:
B() { std::cout<<"reset B"<<std::endl; }
// no override of reset() here
};
class C: public A {
public:
void reset() override {
std::cout<<"reset C"<<std::endl;
}
};
N.B. Aはから派生するクラスを知りませんが、派生クラスにreset()オーバーライドがない場合は、A :: reset()を呼び出すと派生クラスオブジェクトがその構築された状態にリセットされますリセットのオーバーライドがある場合に述べたように、すなわち
A* a = new B();
a->reset(); // -> this equals to *a = B();
ただし、()派生クラスでは、呼び出し元::リセット()は、)(すなわち
A* a = new C();
a->reset(); // -> this should call C::reset()
[奇妙に繰り返されるテンプレートパターン(CRTP)](https://en.wikipedia.org/wiki/Curiously_recurring_template_pattern)を参照してください。 –
私の理解は、これは単にC++のデフォルトの動作であるということですか?私の誤解は何ですか?したがって、Bに固有のリセットが必要な場合は、オーバーライドを書きます。 CRTPを使用して知識を取得するにはBからCRTPを介して派生し、次にB :: resetを呼び出して少し奇妙な音を出します:-) – Klaus
@Klausどのように質問1)を達成しますか? A :: reset()内の私のコメントで? – james