私は基本クラスを持っています:
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?
}
さて、速度を返すための最良の方法は何ですか?