I suppose I understand the meaning of "protected inheritance." However, having discussed this issue with one person here, I feel a little confused now.
Here is my understanding of "secure inheritance" in C ++
Assume the following class structure.
class Base {}
class SubClass : protected Base {}
1> If a subclass is defined as a "protected BaseClass", this subclass is no longer a subclass of BaseClass. Instead, BaseClass serves only as a utility for subclassing. In other words, if you add SubClass * to Base *, SubClass & to Base &, or SubClass to Base, you should expect an error.
2> The main reason people use secure inheritance is because the expected SubClass is NOT a subclass of Base (for example, Car is not a subclass of Engine). Although at the same time, SubClass wants to call functions defined in the base class.
3> There is some reason why you prefer to use protected inheritance rather than defining a member variable as a base object. (but I donβt remember in which case).
Please correct my comments if I am wrong.
Thank you
q0987 source
share