When creating a C++ inheritance structure, you have to define member functions exactly the same in multiple places:
If B is an abstract base class, and D, E, and F all inherit from B, you might have this:
class B
{
virtual func A( ... params ) = 0;
};
class D : public B
{
func A( ... params );
};
/* ... etc... similar implementations for E and F */
So, there is obviously some duplication here. If the interface to B is large, you may have many places to change if the interface needs to change.
A coworker suggested some trickery with an embedded craftily-created #includes, ala:
class D: public B
{
#include "B_Interface.h" // B_Interface.h is a specially crafted .h file
}
This seems a little kludgy? Is it? Is there a better solution to avoid dual maintenance?
Also, maybe the solution here is really better tools to support the language, like Visual Assist X?
Edit: Assume the derived classes must have unique implementations.
Actually, the biggest problem with changing an interface usually is all the code that uses it, not the code that implements it. If it’s easy to change it for the implementer, it would probably make life harder for the users.