I’m trying to design a class hierarchy in C++ where I have a base interface FooInterface
with a pure virtual function foo()
, and another interface FooBarInterface
that should extend FooInterface
and add an additional function bar()
. I then have a class Foo
that implements foo()
from FooInterface
, and a class FooBar
that inherits from Foo
and FooBarInterface
and implements bar()
.
My goal is to have the func
function accept a std::shared_ptr to any object that implements both foo()
and bar()
.
#include <memory>
class FooInterface {
public:
virtual void foo() = 0;
};
class FooBarInterface : public FooInterface {
public:
virtual void bar() = 0;
};
class Foo : public FooInterface {
public:
void foo() override {};
};
class FooBar : public Foo, public FooBarInterface {
public:
void bar() override {};
};
void func(std::shared_ptr<FooBarInterface> fb) {}
int main() {
func(std::make_shared<FooBar>());
}
Note: omitting virtual destructor.
However, this code does not compile probably because that foo()
in FooBarInterface
is not overridden in FooBar
, even though FooBar
inherits from Foo
, which implements foo()
.
How should I modify my code? Or do you have any design pattern that I should refer to?