DEV Community

Rohan Uprety
Rohan Uprety

Posted on

Add new method to Abstract Base Class C++ and Extend them

This question already has answers here:
Pure virtual functions and binary compatibility (4 answers)
Your post has been associated with a similar question. If this question doesn’t resolve your question, ask a new one.

Closed 2 days ago.

(Private feedback for you)

I have a abstract base class . There are many abstract classes which derived from this Abstract Base class

class Base
{
public:
virtual void food() =0
};
for example

class Derived1: public Base
{
};
Suppose i want to add new method to it, without breaking binary compatibility. The only way is to extend this class

class Base_Ext : public Base
{
public:
virtual void food1()=0;
};
The problem is the already existing derived classes implementation wont be able to access this food1(). How to solve this problem How can Abstract Derived classes see this new method

One solution i have in mind is : - Lets say the original Derived class was Derived1......

class Derived1: public Base
{
} ;
Now i would need to extend Derived1.......

class Derived2 : public Derived1, public Base_Ex
{
} ;
again here to solve diamond problem, I will have to change

class Derived1: public Base to

class Derived1: public virtual Base {}
which i dont want to do.. as it would again break binary compatibility of existing derived classes.so stuck at it

Top comments (0)