class IA
{
public:
virtual void a() = 0;
};
class A: virtual public IA
{
public:
virtual void a()
{
}
};
class IB: virtual public IA
{
public:
virtual void b() = 0;
};
class B: virtual public IB, public A
{
public:
virtual void b()
{
}
};
Do you always inherit your interfaces as virtual as I do above? If not, how would you impement the above code?
Using multiple inheritance coupled with virtual inheritance is an appropriate design choice when it comes to separating interface and implementation hierarchies. E.g., see: "When virtual inheritance IS a good design?"
Pros:
Cons:
Alternatives:
no good alternatives if interface hierarchy is needed
otherwise, the hierarchy could be broken into individual interfaces
It could look something like:
struct IA
{
virtual ~IA() = default;
virtual void a() = 0;
};
struct A: public IA
{
virtual ~A() = default;
virtual void a() {}
};
struct IB
{
virtual ~IB() = default;
virtual void b() = 0;
};
struct B: public IB
{
virtual ~B() = default;
virtual void b() {}
};
struct C: public A, public B
{
};
There is one relatively clean workaround. When you inherit B from IB, the compiler requires your provide an implementation of all abstract methods from IB including IA. As a() is already implemented in A, you can create a stub in B that simply invokes a method from A:
class IA
{
public:
virtual void a() = 0;
};
class A: public IA
{
public:
virtual void a()
{
}
};
class IB: public IA
{
public:
virtual void b() = 0;
};
class B: public IB, public A
{
public:
virtual void b()
{
}
virtual void a()
{
A::a();
}
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With