从接口转换为实现接口的类的子类

时间:2016-01-14 09:55:35

标签: c++ casting

我们的项目中有一个Models_Manager类,带有IGameObjects的映射

        std::map<std::string, IGameObject*> gameModelList;

我们添加像这样的对象

Models::Sphere* sphere = new Models::Sphere();
sphere->SetProgram(Shader_Manager::GetShader("textureShader"));
sphere->Create();
gameModelList["fireball"] = sphere;

稍后我们想再次访问该球体。为此,我们希望从IGameObject转换为Sphere。怎么样?谢谢

继承:

class Sphere : public Models::Model
class Model :public IGameObject

1 个答案:

答案 0 :(得分:0)

看起来我需要使用dynamic_cast

Models::Sphere* spherecast = dynamic_cast<Models::Sphere*>(gameModelList["fireball"]);
相关问题