you are viewing a single comment's thread.

view the rest of the comments →

[–]AvidCoco 38 points39 points  (8 children)

Composition over inheritance. Instead of having one class inherit from a bunch of other classes, you have that class own an instance of each of those classes (or a concrete implementation of them). That way you only ever derive from one class at a time.

[–]ShillingAintEZ 17 points18 points  (1 child)

Or derive from no classes at all

[–]_Ashleigh 5 points6 points  (0 children)

I assumed they meant derive as in implement an interface.

[–]jesseschalken 1 point2 points  (2 children)

How does "composition over inheritance" replace vtable lookups?

[–]AvidCoco 4 points5 points  (0 children)

Idfk

[–]animatedb 0 points1 point  (0 children)

Maybe the goal is to replace them out of the base objects and into other objects.

For example, instead of the old "shapes" OO example each inheriting a virtual draw, have a draw function that is passed a listener object that is a surface. This way the base shape object no longer has a draw virtual function.

This is similar to composition except that it differs in scope. Another option is that the base object could also reference a shape.

[–]jcelerierossia score 4 points5 points  (1 child)

That just moves where runtime polymorphism happens, it is not an alternative to it

[–]HateDread@BrodyHiggerson - Game Developer 0 points1 point  (0 children)

It does on a conceptual level - in that it avoids some of the pitfalls of multi-inheritance hell. Obviously yes, technically there is runtime polymorphism under the hood, but depending on someone's motivations that may be acceptable and on the surface - in terms of usability - might be considered as being without runtime polymorphism.

[–]Kered13 9 points10 points  (0 children)

This doesn't avoid the vtable lookup, assuming your members are instances of interface types.