0

私は基本クラスを持っています:

class motorcycle
{
public:

   virtual int speed()
   { return 0; }
}

そして、基本クラスを継承するいくつかのクラス(例では2つだけですが、多くのクラスを持つことができます):

class honda: public motorcycle 
{
public:

   int speed()
   { return 2; }
}

class yamaha: public motorcycle 
{
public:

   int speed()
   { return 1; }
}

派生クラスの1つを指す基本クラスへのポインターがあります。

honda* h = new honda();
...
int speed = get_speed(h);

どこget_speedにありますか:

int get_speed(motorcycle* m)
{
    // How to return speed according to m class?
}

さて、速度を返すための最良の方法は何ですか?

4

1 に答える 1

6
int get_speed(motorcycle* m)
{
    return m->speed();
}
于 2012-07-04T12:35:50.760 に答える