私はEffective C++を読んでいて、この例に出くわしました:
class Window { // base class
public:
virtual void onResize() { ... } // base onResize impl
...
};
class SpecialWindow: public Window { // derived class
public:
virtual void onResize() { // derived onResize impl;
static_cast<Window>(*this).onResize(); // cast *this to Window,
// then call its onResize;
// this doesn't work!
... // do SpecialWindow-
} // specific stuff
...
};
本は言う:
あなたが予期しないかもしれないことは、現在のオブジェクトでその関数を呼び出さないことです! 代わりに、キャストは *this の基本クラス部分の新しい一時的なコピーを作成し、そのコピーに対して onResize を呼び出します!
static_cast (上記のコード) が新しいコピーを作成するのはなぜですか? オブジェクトの基本クラス部分だけを使用しないのはなぜですか?