Search code examples
c++inheritanceprivateprotectedc++-faq

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


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 Derived2 : protected Base;

Is it really useful?


Solution

  • It is useful when you want to have access to some members of the base class, but without exposing them in your class interface. Private inheritance can also be seen as some kind of composition: the C++ faq-lite gives the following example to illustrate this statement

    class Engine {
     public:
       Engine(int numCylinders);
       void start();                 // Starts this Engine
    };
    
    class Car {
      public:
        Car() : e_(8) { }             // Initializes this Car with 8 cylinders
        void start() { e_.start(); }  // Start this Car by starting its Engine
      private:
        Engine e_;                    // Car has-a Engine
    };
    

    To obtain the same semantic, you could also write the car Class as follow:

    class Car : private Engine {    // Car has-a Engine
     public:
       Car() : Engine(8) { }         // Initializes this Car with 8 cylinders
       using Engine::start;          // Start this Car by starting its Engine
    }; 
    

    However, this way of doing has several disadvantages:

    • your intent is much less clear
    • it can lead to abusive multiple inheritance
    • it breaks the encapsulation of the Engine class since you can access its protected members
    • you're allowed to override Engine virtual methods, which is something you don't want if your aim is a simple composition