Look at the following code:
class Base
{
int a;
public:
Base(int b){a =b;}
};
class M1: public virtual Base{
public:
M1(int a): Base(a+10){} // Expect a is increased by 10
};
class M2: public virtual Base{
public:
M1(int a): Base(a+20){} // Expect a is increased by 20
};
class F: public M1, public M2{
public:
F(int a): M1(a-2), M2(a-3), Base( a-10){} // ouch Base constructor called only once and with unexpected value!
};
Now while the code is really stupid, it highlights one problem, basically both classes M1 and M2 to working correctly assumes that Base is in a particular state (in this case it is increased by 10 or 20). Adding another derived class (F) breaks this encapsulation because Leaves "a" in a unexpected state because it decrease it by 10 instead of increasing.
M1 and M2 will access "a" with unexpected value then, to me this means that basically I breaked encapsulation, people is no longer free to change code in M1,M2 classes because it could eventually break F (also viceversa is true).
Actually what I'm asking for is the exact opposite of the
In the fragile base class problem we have "derived" class that is broke by base class changes, in my case it is the opposite:
- I have a derived class that is breaking one of the base classes.