C ++: avoiding double maintenance in inheritance hierarchies

When creating a C ++ inheritance structure, you must precisely define the member functions in different places:

If B is an abstract base class, and D, E, and F all inherit from B, you can have the following:

class B
{
   virtual func A( ... params ) = 0;
};

class D : public B
{
   func A( ... params );
};

/* ... etc... similar implementations for E and F */

So here, obviously, there is some overlap. If the interface to B is large, you can have many places to change if the interface needs to be changed.

An employee suggested some tricks with the built-in dodgy #includes, ala:

class D: public B
{
   #include "B_Interface.h"  // B_Interface.h is a specially crafted .h file
}

Does this seem a bit tattered? It? Is there a better solution to avoid double maintenance?

Also, maybe the solution here is really the best language support tools like Visual Assist X?

Edit: Suppose derived classes must have unique implementations.

+3
5

, , , . , , , .

+12

; .

+4

, , - , Visual Assist X?

. - .

+4

If you need to implement them many times using the default behavior, perhaps they should just be virtual, not pure virtual.

+1
source

Instead of using the preprocessor for another way, you should not use it, I would try my editor (or IDE, if that is what you are interested in).

+1
source

Source: https://habr.com/ru/post/1734948/


All Articles