Multiple Inheritance Problem

Get answers to all your basic programming questions. No Ogre questions, please!
User avatar
TWO
Halfling
Posts: 61
Joined: Sat Jan 26, 2008 9:30 pm
Location: Germany

Multiple Inheritance Problem

Post by TWO »

Code: Select all

// Pure virtual interface
class IPhysicsBody { virtual void addToScene() = 0; };
class IPhysicsRigidBody : public IPhysicsBody {};
class IPhysicsDynamicRigidBody : public IPhysicsRigidBody {};

// Implementation
class IPhysicsPhysXRigidBody : public IPhysicsRigidBody { virtual void addToScene(); };
class PhysicsPhysXDynamicRigidBody : public IPhysicsDynamicRigidBody, public IPhysicsPhysXRigidBody {};
Instance of PhysicsPhysXDynamicRigidBody can't be created, because IPhysicsBody::addToScene is abstract. Yet there is an available implementation in IPhysicsPhysXRigidBody which the class inherits too. I could implement PhysicsPhysXDynamicRigidBody::addToScene() and call the parent implementation this way, but that's alot of extra work and deforms the implementation. Is there a way to solve this? Maybe rearranging my class hierarchy? This is the first time I've encountered this problem.

Thank you.
bstone
OGRE Expert User
OGRE Expert User
Posts: 1920
Joined: Sun Feb 19, 2012 9:24 pm
Location: Russia
x 201

Re: Multiple Inheritance Problem

Post by bstone »

The short answer is no. The long answer is you could take two paths: 1) use virtual inheritance but that has its own nuances which might cause you more trouble than it's worth; 2) use static polymorphism based on templates but that means turning most of your classes into templates with all related consequences.
User avatar
TWO
Halfling
Posts: 61
Joined: Sat Jan 26, 2008 9:30 pm
Location: Germany

Re: Multiple Inheritance Problem

Post by TWO »

Thank you for your quick reply Sir. I didn't even know virtual inheritance, will take a look at it. Templates would over-engineer the problem here, i guess.