Say I have 4 classes:
class I { public: virtual void X() = 0; };
class A : public virtual I { public: virtual void X() { } };
class B : public I { };
class C : public A, public B { };
I, B and C are abstract, where as A is not. If I simply add virtual to the inheritance of I for B, then A::X() resolves I::X() in C.
However, I cannot change the source of B.
My question: Can I get A::X() to resolve I::X for C without being able to change B? I have tried declaring A and B to be virtual to C to no avail. I am trying to have no redundant code (e.g. have C declare X() { A::X(); }). Any neat hacks?
Also – there are a few questions very much like this, but I couldn’t find any talking about using virtual inheritance. Please point to me one if I missed it.
This is quite good: When virtual inheritance IS a good design?
The problem here is that in C you have two interfaces I. That is why A::x() satisfies
its interface I – but it cannot make not abstract interface I from class B.
For C the only way to have exactly one interface of I – is to change B to derive from I virtually – in this way both I interfaces from A and from B will be merged to one in C. You cannot change B – so the only way is to add this redundant code which you are trying to avoid. I mean define C::X().