
Is it OK to use multiple inheritance if one parent class is an interface (only contains pure virtual functions with virtual destructor)?
I want to expose only interface part (yellow class in the picture) to increase compile speed. Green part is implementation part. But CPet should inherit from CAnimal(is-a relation) and IPet(implement), there are “Diamond of Death” 🙁
Interface classes (in yellow) only have pure virtual functions and virtual destruction, so when I create CDog, CCat through factory class, there are no problem like ambiguity. CDog has two vtables (from IDog and CPet) but in the virtual function tables, the points indicate same function (CDog member functions).
There’s no compile error, no running error… but I’m worry about this hierarchy.
Is it OK or are there any problems?
PS : I don’t want to use ‘virtual inheritance’ because if I use that, I can’t look into class member variable through watch view.(I guess it is because virtual inheritance link to parent class like linked-list.)
Environment : Visual Studio C++ 2008 or over.
Given the description above, you should not be able instantiate an instance of
CPetbecause the pure virtual functionIAnimal::isAlive()is undefined in theIPetvtable.Produces the following when compiled with Visual C++ 2008 & 2010:
GCC produces a similiar warning: