Why protected and private inheritance are defined and proposed? I understand some cases private inheritance could be used but it is not recommended. How about protected inhe
This is a situation i would use Protected inheritance
Base -> Derived1 -> Derived2
I generally don't use protected
inheritance. In fact, I don't generally use private
inheritance. If something does not satisfy the Liskov Substitution Principle then I don't see a reason to use inheritance of any kind; and if it does satisfy LSP then you use public
inheritance.
However, the language distinguishes between private
and protected
only from the class's point of view (that is, code using the class can't tell the difference).
You should use protected
inheritance when you want it's semantics, and you should use private
when you don't want protected
.
See Herb Sutter's Uses and Abuses of Inheritance, Part 1, which is also in his More Exceptional C++ book.
Private inheritance is usually used for mixins---where people inherit to get functionality from the base class, rather than because of "is-a" inheritance.
Protected inheritance can also be used for mixins, where the mixed-in functionality is to be available to downstream classes too.