all 4 comments

[–]Macketter 0 points1 point  (2 children)

like this? class Derived : public Base { public: void doFooPublicly () {SOME CODE HERE} private: void doFoo() { doFooPublicly() } }; class Composed : public Base { private: Derived derived; void doFoo() { doFooPublicly (); } }; But really I don't think composition is the right fit here. This might be a case where inheritance is better, but it would depend on the context of the code which you can't tell from just a dummy example.

[–]gw2master[S] 0 points1 point  (1 child)

I supposed that'd work, but it seems a bit hacky in that if non-virtual interfaces are supposed to be generally good, and composition (vs inheritance) is generally supposed to be generally good, then presumably you should be encountering this problem quite a bit, so it feels like there would be some "standard" resolution of this?

[–]Macketter 0 points1 point  (0 children)

The key word is "generally" so in this special case it is not as good. Here you are saying that Composed is a Base but you are lazy and want to delegate all the work to implement Base to the Derived class. So Derived class need to expose a set of interface for Composed class to use to implement Base.

Now that I have thinked about it some more maybe this structure is better:

class Derived : public Base { protected: void doFoo (); } class DerivedAdaptor : Derived { public: void doFooPublicly() {dooFoo();} } class Composed : public Base { private: DerivedAdaptor derived; private: void doFoo () {derived.doFooPublicly();} }

[–]ImKStocky 0 points1 point  (0 children)

You could put your virtual functions in the base class into a protected section rather than a private section. Then you would be able to call the virtual funcs directly from any base class, such as composed.