3

Could someone explain what exactly private/protected inheritance in C++ is for, and what problem(s) it is intended to solve?

class Bar { };
class Foo : private Bar { };

I've already seen this question but I still don't understand what it is, let alone when I should use it.

(Comparison to features in Java/C# or similar languages could also be helpful.)

Community
  • 1
  • 1
user541686
  • 205,094
  • 128
  • 528
  • 886
  • 3
    BTW Most of the time you'd think of using private inheritance, forget it and use composition. – Kos Nov 26 '11 at 20:48

4 Answers4

5

The private inheritance models "is-implemented-in-terms-of". The meaning is similar to "has-a". The differences are:

  1. With private inheritance you don't need to write a wrapper (good for lazy programmers)

  2. "has-a" allows you better control, you can expose only a subset of the interface or change method names if you like.

  3. Private inheritance makes exception safety difficult, have a look at exceptional c++ for more information

  4. You really need private inheritance just when you want to use a protected members of your base class.

  5. Sometimes private inheritance is used in the mix-in class (Effective c++ memory management chapters)

My personal preference is using "has-a" for general purpose, I use private inheritance just after I have rule out other options.

sehe
  • 374,641
  • 47
  • 450
  • 633
Alessandro Teruzzi
  • 3,918
  • 1
  • 27
  • 41
2

To understand private inheritance, you must first understand public inheritance.

If you have the following:

class Base
{
public:
  void SomeFunc();
};

class Pub : public class Base {};

This means that the following is allowed:

Pub aPub;
Base &basePart = aPub;

You can take the derived class and get a reference (or pointer) to a base class. You can also do this:

Pub aPub;
aPub.SomeFunc();

That which was public in Base is public in Pub.

Public inheritance means that everyone, everywhere can access the base class components of the drived class.

Private inheritance is a bit different:

class Priv : private class Base {};

Priv aPriv;
Base &basePart = aPriv;

That last line does not work in general. Private inheritance means that the only functions and classes that can perform this operation are either direct members of Priv or friends of Priv. If you write this code outside of a Priv member or friend, it will fail to compile.

Similarly:

Priv aPriv;
aPriv.SomeFunc();

Only works if this is within a member or friend of Priv.

Private inheritance means that only members or friends of the derived class can access the base class components. So member functions can call the base class part of the derived class, but nobody in the outside world can.

When you publicly derive a class from another class, you are saying that the derived class "is a" base class. It is exactly one of those types. When you privately derive from a base class, you are hiding the derivation from the outside world. This means that you are using the implementation in the base class to implement your derived class, but nobody else needs to know it. The derived class is "implemented in terms of" the base class.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • 1
    +1 Adds some value (_`you are hiding the derivation from the outside world`_). However it could have been worded much more to the point. Also, I'm pretty sure @Mehrdad does already _understand public inheritance_ :) – sehe Nov 26 '11 at 20:56
1

Private inheritance is a way of implementing composition of classes. This C++ FAQ has a whole chapter about this topic, see http://www.parashift.com/c++-faq-lite/private-inheritance.html.

fschoenm
  • 1,391
  • 13
  • 32
0

It doesn't solve problems, but it allows you more code design options, in this case all methods from Bar are private in Foo.

nikola-miljkovic
  • 652
  • 4
  • 14