I have a common interface that many classes in my project inherit (FWIW it’s a ref-counting interface). This interface is inherited by both classes and other interfaces. Instead of re-implementing the interface in every class, I’d like to just have a corresponding implementation class that all the other classes inherit. The problem is the way I have it laid out leads to a diamond-of-death.
struct IBar {
virtual VOID Bar() = 0;
};
struct CBar : IBar { // implementation class for IBar
VOID Bar() { }
};
struct IFooBar : IBar {
virtual VOID Foo() = 0;
};
struct CFooBar : IFooBar, CBar { // implementation of IFooBar interface
VOID Foo() { }
// Bar() should be implemented by the inheritance of CBar
};
If I try to compile, I get the following error:
error C2259: 'CFooBar' : cannot instantiate abstract class
1> due to following members:
1> 'void IBar::Bar(void)' : is abstract
I realize I can fix this by implementing CFooBar::Bar() { return CBar::Bar(); } or having IFooBar inherit from CBar but I suspect there’s a less hacky way of solving this. Any suggestions would be greatly appreciated.
You could try always virtually inheriting from IBar. That way, you cut out the middleman, so to speak, and always directly inherit from it regardless if you also inherit from one of its decendents.
Eg:
I’d always recommend using
classoverstructif you are writing any member functions – such a thing is, after all, by definition a class. Also, it’s worth explicitly setting access modifiers as in my example (the above code doesn’t actually compile without adding them).