C++ 调用派生类方法

C++ 调用派生类方法,c++,c++11,polymorphism,C++,C++11,Polymorphism,我有一个基本类: class motorcycle { public: virtual int speed() { return 0; } } 还有一些继承基类的类(示例中只有2个,但我可以有很多类): 我有一个指向基类的指针,它指向一个派生类: honda* h = new honda(); ... int speed = get_speed(h); 其中get\u speed为: int get_speed(motorcycle* m) { // How to re

我有一个基本类:

class motorcycle
{
public:

   virtual int speed()
   { return 0; }
}
还有一些继承基类的类(示例中只有2个,但我可以有很多类):

我有一个指向基类的指针,它指向一个派生类:

honda* h = new honda();
...
int speed = get_speed(h);
其中
get\u speed
为:

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

现在,返回速度的最佳方法是什么?

speed
不是一个纯粹的虚拟方法。@Nick它是虚拟的,这就足够了。多态性的工作不一定是纯粹的。“你为什么会这么想?”卢钦格里戈,因为我错了,谢谢。但如果它不是虚拟的?@Nick,那么你的设计就失败了。你可以在施法后调用它或其他一些难看的解决方法。
speed
不是纯粹的虚拟方法。@Nick它是虚拟的,这就足够了。多态性的工作不一定是纯粹的。“你为什么会这么想?”卢钦格里戈,因为我错了,谢谢。但如果它不是虚拟的?@Nick,那么你的设计就失败了。你可以称之为铸造后或其他一些丑陋的工作。
int get_speed(motorcycle* m)
{
    return m->speed();
}
int get_speed(motorcycle* m)
{
    return m->speed();
}