Private inheritance in C++

C++ standard supports three types of inheritance: public, protected and private. Usage of public inheritance is pretty common; but what about the other ones? This post will discuss private inheritance.

What’s the difference between private and public inheritance in C++? Unlike public inheritance, private inheritance is not an “is-a” relationship.

Such piece of code will not compile:

class Base
{
};

class Derived: private Base 
{
};

void doSmthOnBase(const Base& base){}

Derived d;
doSmthOnBase(d); // error !

The compiler will complain about error: ‘Base’ is an inaccessible base of ‘Derived’ doSmthOnBase(d). Of course changing ‘private’ to ‘public’ would allow this code to compile correctly.

Second difference is the scope of derived members/methods: all members and methods from base class (protected and public) will become private in derived class. Private members from base class will not be accessible from derived class.

So what’s the use of private inheritance?

Private inheritance means “has-a” relationship (or: “is implemented in terms of”). Similar to composition, you should use private inheritance when you want to make use of the implementation details of base class, but you do not want to inherit the interface.

When private inheritance is better than composition?

You will have to use private inheritance when you need to have access to protected members/member functions of base class. In other cases almost always you should use composition. Private inheritance will make your code harder to maintain and increase the compilation dependencies (in case of composition, you can use forward declaration and a member-pointer to object).

Empty Base Optimization (EBO)

There is one more specific case when private inheritance can be reasonable: so called Empty Base Optimization. Imagine EmptyClass with zero non-static members and zero virtual functions nor base classes. Composition of EmptyClas will increase the size of OtherClass_composition.

class EmptyClass{};
class OtherClass_composition 
{ 
    private: 
        EmptyClass m_empty; 
        int m_member;
};

But if you use private inheritance:

class OtherClass_inheritance : private EmptyClass
{   
    private: 
        int m_member;
};

the size of OtherClass_inheritance should not increase (but it’s dependant on the compiler). With the use of MinGW 4.9.1 the size of OtherClass_composition was 8, while the size of OtherClass_inheritance was 4.

Read more about private inheritance on isocpp webpage and take a look on Composition over inheritance rule.

Advertisements

3 thoughts on “Private inheritance in C++

  1. Dybon says:

    Hi Kate!
    Thanks for clearing things up about private ihnheritance. However I have the question where this type of inheritance should be used in real life situation. Did you apply this technique in your career already?Looking forward to your reply

    Like

  2. katecpp says:

    Hello Dybon,
    I personally did not design the class with private inheritance yet (it’s not very common; usually composition works perfectly) but I worked with legacy code that had designs like that. It is good to understand why these things were designed this way. In the real life situations you should use private inheritance when:
    – you want to override some virtual functions, but you don’t want to inherit whole interface,
    – you need to have an access to protected functions, but you don’t want to inherit the whole interface,
    – you want to inherit from a class that does not have virtual destructor – public inheritance would allow to use Derived class via Base class pointer,
    – also: it is used in Class Adapter pattern to interface adaptations: I found very comprehensive reading about it, if you are interested in some details take a look: https://blog.feabhas.com/2012/05/interface-adaption-and-private-inheritance/

    Have a nice day!
    Kate

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s