Jump to content

  • Log In with Google      Sign In   
  • Create Account

C++ : multiple inheritance & casting

  • You cannot reply to this topic
1 reply to this topic

#1 Max Power   Members   -  Reputation: 208

Like
0Likes
Like

Posted Today, 04:56 AM

I was wondering if I shouldn't start using multiple inheritance for my classes, but I don't have any experience with it. Imagine something like this:

 

 

 

struct GraphicalObject;

struct PhysicalObject;

struct StaticObject : public PhysicalObject;

struct DynamicObject : public PhysicalObject;

 

struct Particle : public GraphicalObject;

struct Box : public GraphicalObject, public DynamicObject;

 

void render (GraphicalObject* obj);

void applyImpulse (DynamiclObject* obj);

void setCollisionFilter (PhysicalObject* obj);

 

 

Of course, I can pass a particle to render, but can I pass a box to any of the functions just like that?

 

 

And what if I want to store pointers? Like this:

 

vector<PhysicalObject*> v;

 

Can I simply put box objects there?...


Edited by Max Power, Today, 04:59 AM.


Sponsor:

#2 wintertime   Members   -  Reputation: 1887

Like
1Likes
Like

Posted Today, 06:24 AM

Thats a typical, but bad idea. The rigid inheritance structure will give you problems when you need one aspect of a class without another thats already inherited, causing code duplication or bloated objects or objects with deactivated parts.
You better just have independent components like Position, BoundingBox, Mesh for graphics and others with data for the game logic. Then you can combine these as needed through composition into game objects or just have some arrays of these components, depending on your needs.





PARTNERS