我已经阅读了很多关于虚拟功能的内容,但是我仍然无法获得某些功能来满足我的需求。Overriden虚拟功能
基本上,我有下面的类:
class Body
{
protected:
scene::ISceneNode* Model;
virtual void setModel();
public:
Body(core::vector3df Position, core::vector3df Rotation);
};
Body::Body(core::vector3df Position, core::vector3df Rotation)
{
CurrentThrust = 0;
setModel();
Model->setPosition(Position);
Model->setRotation(Rotation);
}
void Body::setModel()
{
Model = Engine::Instance->GetSceneManager()->addCubeSceneNode();
Model->setMaterialFlag(video::EMF_LIGHTING, false);
}
我创建新类继承车身,这种想法是,我重写“则setModel()”中的类和构造函数将加载我的新模型,而不是默认的;像下面
class Craft : public Body
{
protected:
virtual void setModel();
public:
Craft(core::vector3df Position, core::vector3df Rotation);
};
Craft::Craft(core::vector3df Position, core::vector3df Rotation) : Body(Position, Rotation)
{
// Other stuff
}
void Craft::setModel()
{
Model = Engine::Instance->GetSceneManager()->addAnimatedMeshSceneNode(Engine::Instance->GetSceneManager()->getMesh("resource/X-17 Viper flying.obj")); // addCubeSceneNode();
Model->setMaterialFlag(video::EMF_LIGHTING, false);
Model->setScale(core::vector3df(0.1f));
}
然而,它始终会创建一个立方体的模型,而不是我的蝰蛇模式,当我创建工艺的新实例。
是否有可能让虚拟功能像我在想的那样工作?或者我是否需要更改我的构造函数以在各自的类中创建模型?
感谢
你应该避免在构造函数中调用虚函数http://stackoverflow.com/questions/962132/calling-virtual-functions-inside-constructors – mathematician1975
你如何创建对象?请向我们展示使用情况,这是这种情况下非常重要的部分。 –