0

I have 3 classes MovableObject, FixedObject and CollisionObject. A CollisionObject should have the possibility to be either a MovableObject or a FixedObject. But it would make no sense to use multiple inheritance, as it can't be both at the same time. Basically, if I create a Projectile, the hierarchy would be:

Sprite <- MovableObject <- CollisionObject <- Projectile

And if I create an Obstacle it would be:

Sprite <- FixedObject <- CollisionObject <- Obstacle

(My base class is Sprite)

So what CollisionObject should inherit from is decided by what the child objects inherits from (Either a Movable- or FixedObject). But how do I implement this in C++ in a nice way?

Quentin
  • 62,093
  • 7
  • 131
  • 191
Bonbin
  • 305
  • 1
  • 6
  • 19
  • 1
    "But i cant inherit from both of the classes ofcourse" Why not? –  Jan 21 '17 at 14:00
  • 1
    "But i cant inherit from both of the classes ofcourse." -> actually, __you can__. This is called [multiple inheritance](http://www.learncpp.com/cpp-tutorial/117-multiple-inheritance/). – ForceBru Jan 21 '17 at 14:01
  • @latedeveloper well if i do CollisionObject : public FixedObject, public MovableObject wont it create an instance of both? – Bonbin Jan 21 '17 at 14:02
  • @Bonbin http://stackoverflow.com/questions/21558/in-c-what-is-a-virtual-base-class –  Jan 21 '17 at 14:03
  • @ForceBru i know its possible but wont i inherit members from both classes always? I only want to inherit from one of them – Bonbin Jan 21 '17 at 14:05
  • To be fair you *can* inherit from both, but it would make no sense indeed. – Quentin Jan 21 '17 at 14:05
  • It sounds like the inheritance here is upside down. – Pete Becker Jan 21 '17 at 14:36
  • This could be a case where the inheritance isn't the right solution among Sprite, MovableObject, and FixedObject. Maybe some of those properties should be driven by data (members or sub-objects). Then Projectile and Obstacle could inherit from whatever that became and just set the data appropriately upon construction. – TheUndeadFish Jan 21 '17 at 15:07

2 Answers2

2

Disclaimer: the usefulness of this answer will vary wildly depending on the rest of your design.

That said, "A CollisionObject should have the possibility to be either a MovableObject or a FixedObject" does not sound like you need a CollisionObject to somehow switch its base class, but instead to be the base class.

struct CollisionObject { };
struct MovableObject : CollisionObject { };
struct FixedObject : CollisionObject { };
Quentin
  • 62,093
  • 7
  • 131
  • 191
  • I forgot to mention that i have Sprite as a base class and that An object that does not need collision would inherit directly from either movable or fixedObject. Thanks for the answer – Bonbin Jan 21 '17 at 14:16
  • @Bonbin so the "moveability" and the "collideability" of an object are orthogonal properties -- unfortunately this can't be modeled with an OOP class hierarchy. Consider composition or policy-based design. – Quentin Jan 21 '17 at 14:22
2

If CollisionObject is a class, then it will always inherit from the same classes, so what you request is not possible if you do not want to use multiple inheritance. But multiple inheritance would make CollisionObject both movable and fixed, which does not sound very right.

However, if you make CollisionObject a template, it can be done:

template<typename Base> class CollisionObject : public Base
{
   ...
};

class Projectile : public CollisionObject<MovableObject> {...};
class Obstacle : public CollisionObject<FixedObject> {...};
Wojtek Surowka
  • 20,535
  • 4
  • 44
  • 51