Why do we actually need Private or Protected inheritance in C++?

后端 未结 7 1008
情话喂你
情话喂你 2020-11-28 01:44

In C++, I can\'t think of a case in which I would like to inherit private/protected from a base class:

class Base;
class Derived1 : private Base;
class Deri         


        
7条回答
  •  情深已故
    2020-11-28 02:23

    Public inheritance models IS-A.
    Non-public inheritance models IS-IMPLEMENTED-IN-TERMS-OF.
    Containment models HAS-A, which is equivalent to IS-IMPLEMENTED-IN-TERMS-OF.

    Sutter on the topic. He explains when you'd choose non-public inheritance over containment for implementation details.

提交回复
热议问题