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

后端 未结 7 979
情话喂你
情话喂你 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:45

    Private inheritance is mostly used for wrong reason. People use it to IS-IMPLEMENTED-IN-TERMS-OF, as indicated in an earlier answer, but in my experience it's always more clean to keep a copy rather than inherit from class. Another earlier answer, the one about CBigArray, provides a perfect example of this anti-pattern.

    I realize that there may be cases when has-a does not work due to over-zealous use of "protected", but it's better to fix the broken class than to break a new class.

    0 讨论(0)
提交回复
热议问题