jmasterx
jmasterx

Reputation: 54113

Help with this casting issue

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, the pointer might be different due to multiple inheritance.

Upvotes: 2

Views: 83

Answers (1)

Tom Kerr
Tom Kerr

Reputation: 10720

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.

Upvotes: 3

Related Questions