Which way is better? inheritance or member?

Which way is better? So, for example, let's say we have a game engine class through which you can add objects. But object manager class does that. So should I put it as it's member or inheritance it?

inheritance:

class Engine : public ObjectManager {

}

or as its member:

class Engine {

ObjectManager objectmanager;

public:

ObjectManager& getObjectManager();

}

first:
Engine engine;
engine.addObject();

or second:
engine.getObjectManager().addObject();

which way is better? Thanks :)!
It depends. This one has some thoughts on the matter:
https://stackoverflow.com/questions/49002/prefer-composition-over-inheritance
A member. Especially in C++ and other statically typed languages.
(Should we be able to substitute an object of type Engine where an object of type ObjectManager is expected?)

See: https://en.wikipedia.org/wiki/Liskov_substitution_principle

If you must use inheritance (less boiler plate code to write if Engine must reuse, and expose in its interface, a lot of the functionality of ObjectManager), use private inheritance (ie. inheritance without substitutability).
Last edited on
As far as I know, inheritance represents an is-a relation. In this case, I wouldn't use inheritance, as I see no logican explanation for having an ObjectManager as a parent for an Engine.
Thanks!!!!
Topic archived. No new replies allowed.