思ったところの関数が呼ばれないので理解できていないのかなと思ってます
下のを実行すると
Stage1::A
と出力されるんですが
State::ChangeState
も出力されるんではないかなと思っていたのですが、出力されません
これはどういうことか説明してくれる方いませんでしょうか?
また、2つ質問して悪いのですが、私の持っている参考書ではサブクラスがvirtual関数になってます
サブクラスをvirtual関数にする理由などあったら教えていただけると幸いです
#include <iostream> class Connection { }; class State { protected: void ChangeState( Connection*, State* ); public: void A( Connection* ); void B( Connection* ); void C( Connection* ); void D( Connection* ); }; void State::ChangeState( Connection *t, State *s ) { printf( "State::ChangeState\n" ); } void State::A( Connection* ) { printf( "State::A\n" ); } class Stage1 : public State { public: static State* _instance; State* Instance() { if( _instance == 0 ) _instance = new Stage1; return _instance; } virtual void A( Connection* ); virtual void B( Connection* ); }; State* Stage1::_instance = 0; void Stage1::A( Connection* t ) { ChangeState( t, Stage1::Instance() ); } void Stage1::B( Connection* t ) { } int main() { State *stage1 = new Stage1; Connection *connection = new Connection; stage1->A( connection ); return 0; }