开发者

Help with this casting issue

开发者 https://www.devze.com 2023-03-19 15:23 出处:网络
I\'m making a game where pretty much everything inherits from Entity. I also have an IRenderable interface for all entities that need to be rendered.

I'm making a game where pretty much everything inherits from Entity. I also have an IRenderable interface for all entities that need to be rendered.

When loading the level, every entity that is IRenderable is placed in a vector of IRenderable* which is then passed to the renderer.

When I instance any Entity, I add it to a vector of Entity* .

What I'm wondering is what exactly should I do when this entity sends an ENTITY_DESTROYED message?

The problem is that I do not know if this Entity is IRenderable. I'd have to try and cast it for that and I'm not sure if casting is a good idea here.

What else could I do to avoid casting?

Thanks

Another point to note, even if I know it is IRenderable,开发者_StackOverflow中文版 the pointer might be different due to multiple inheritance.


This article is the best discussion which I've found on the subject. I recommend it.

Otherwise, this may be useful.

class IRenderable;

class Entity {
public:
  virtual ~Entity() {}
  virtual IRenderable *getIRenderable() {return 0;}
}

class IRenderable : public Entity {
public:
  virtual IRenderable *getIRenderable() {return this;}
};

I'd call this solution a band-aid.

0

精彩评论

暂无评论...
验证码 换一张
取 消