C ++ - Questions about "secure inheritance"

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

+3
source share
2 answers

1. - SubClass Base. - . , SubClass Base .

SubClass sub;
Base* base(&sub);

C2243: ' cast': 'SubClass *' 'Base *' ,

2. 3. - , , - public/protected Base SubClass. SubClass . private, Base SubClass - 2, Base SubClass.

+4

, OO, IS-A ( ) HAS-A (). "-AS-A". , AS-AN Stack, , Array Stack.

, , .

0

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


All Articles