protected inheritance

前端 未结 4 1448
眼角桃花
眼角桃花 2021-01-11 19:30

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

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-11 20:08

    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.

提交回复
热议问题