I’m trying to use multiple inheritance to solve a complex hierarchy that I’m developing. The situation is the following:
class A {
virtual void foo();
}
class B {
virtual void foo();
}
class C : public B {
}
class D : public A, public C {
void foo() { ... }
}
class ClientClass {
void method() {
A *a = new D();
a->foo();
}
What I wonder is: will D have just one function foo() in the end? I’m thinking about it because the method is virtual in both parents so they should like collimate onto the same one, but I thin this just because I come from Java and I feel that it may be different in C++. I have to declare the virtual function foo() twice because ClientClass doesn’t know B or C but just A. This is a requirement I would like to keep.
EDIT: does the same answer apply even if both foo() in A and B are pure virtual? (eg = 0)
Actually, the implementation of
D::foowill override bothA::fooandB::fooat the same time.Both inherited functions will still be available, of course, to be called by their full name. But that’s not different than in the single inheritance case.
About the
ClientClass, you create aDobject and then callfoothrough a pointer toA. So theD::foooverride will be called.If you want different versions of
foofor the override ofA::fooandC::foo(for example, because they are unrelated but happen to be called the same way) then you will need a bit of work:Now the use: