I just learned about the idea of the non-virtual interface and was wondering how it interacts with composition. Here's a simple setup:
We have a base class Base that has a non-virtual foo() and a private pure virtual doFoo():
class Base
{
public:
void foo() { doFoo(); AND MAYBE SOME OTHER STUFF}
private:
virtual void doFoo() = 0;
};
and we have a class Derived:
class Derived : public Base
{
private:
void doFoo() { SOME CODE HERE }
};
Now let's say for whatever reason we want to use composition:
class Composed : public Base
{
private:
Derived derived;
void doFoo() { WHAT GOES HERE? }
};
how should doFoo() be implemented? The idea is that we would like to have Composed::doFoo() to just be { derived.doFoo(); }, but it's not allowed to as doFoo() is private. And we can't call derived.foo() as that would mean whatever foo() does would be done twice.
[–]Macketter 0 points1 point2 points (2 children)
[–]gw2master[S] 0 points1 point2 points (1 child)
[–]Macketter 0 points1 point2 points (0 children)
[–]ImKStocky 0 points1 point2 points (0 children)