Representing entities with MI

I'm trying to make a space shooter and I want to make the code very reusable. So far I've been making a bunch of objects with multiple inheritance like this:

entity: anything which can be drawn, has a position and an orientation
solid: any entity which can collide with something else
mobile: any entity which can move
spriteEntity : an entity drawn using a sprite
vertexEntity : an entity drawn out of vertexes

The entity class has the virtual method draw, which gets defined by either spriteEntity or vertexEntity. mobile and solid virtually inherit from the entity class.

Then I could create a projectile class which inherits from solid and mobile but adds methods to damage stuff it collides with, and multiple subclasses which inherit from both projectile and spriteEntity... But I'm not sure if it's a good idea to go this deep in multiple inheritance for game objects. I know MI has a bad reputation.

How would you do this? It's a hard question, but I know that some objects of the game will be sprites and some will be vertexes, and I also know there will be stuff which collides and doesn't move... Or should I make the mobile class a subtype of solid? I am clueless...
Multiple inheritance usually has no problems, as long as you are using abstract base classes, i.e. Classes that have a entirely pure virtual functions, no data, and a virtual destructor. In your example, entity could probably be an abstract base class, which means that it doesn't matter that projectile inherits from mobile and solid, because having them both inherit from an abstract base class means that it doesn't matter that otherwise a double dependency would be assumed (no data would be duplicated, and entitys can't be created at all).

Also, do spriteEntity and vertexEntity have to be classes that are inherited from? Couldn't they just be structs that define a function to draw, and take as parameters the data to be drawn? Couldn't they also be friends to entity and simply be called to do drawing? This would avoid having to do multiple inheritance at all.

This isn't quite perfect (you could probably implement solid and mobile independantly), and there a bunch of other things that could be done, but hopefully this will give you an idea of what you can do to prevent issues that otherwise would probably appear within your code.

Something that I find helps is drawing diagrams of your classes, so that way you can see if any issues will arise, and how you should implement them.

Good luck with your game!
Thanks for the answer! This is one of my first big game with physics in C++, so I'm using SFML. I know it's not the most efficient graphical engine, but it gets the job done. SFML has two ways to draw, vertexes and sprites. Each entity needs either a sf::Sprite or a sf::VertexArray to be drawn, which is why I have two subclasses, each with its own additional member and draw() definition. However, the position and orientation are stored in the entity class, which is then virtually inherited by the 4 other classes to avoid the diamond problem. Also, mobile adds speed, rotation and acceleration vectors.

But if I understood virtual inheritance correctly, as long as my 4 subclasses all virtually inherit from the entity class, they won't include multiple versions of the entity members. So I can then create say, a player class which inherits from solid, spriteEntity and mobile without a problem... right?
Last edited on
> as long as my 4 subclasses all virtually inherit from the entity class,
> they won't include multiple versions of the entity members. ... right?

Right. There would be only one base class object of type entity, if every derived class involved in the MI scenario derives virtually from entity.
If I understood this correctly, I could do this...


+------+----- entity ------------+
| | | |
| | | |
v v v v
mobile solid vertexEntity spriteEntity
+ + +
| | |
| | |
| | |
+------+----> enemy <------------+


Where entity is virtually inherited by mobile, solid, vertexEntity and spriteEntity, but enemy fully inherits from mobile, solid and vertexEntity (not virtual). Is that right?

EDIT: aww, this website doesn't want to show this ascii properly...
Last edited on
> Where entity is virtually inherited by mobile, solid, vertexEntity and spriteEntity,
> but enemy fully inherits from mobile, solid and vertexEntity (not virtual).
> Is that right?

Yes. There would be only one entity base class object.

How enemy inherits (virtually or non-virtually) mobile, solid and vertexEntity would affect the sharing of the mobile, solid and vertexEntity base class objects, if enemy now participates as one of the base classes in a multiple inheritance relationship.
Oh, I see! That's a lot clearer. Making the diamond graphics helps too. Thanks both of you, and merry Christmas if you celebrate it!
Topic archived. No new replies allowed.