17

Public inheritance is easy.

A : public B means every A is a B. In most programming language, like vb.net and objective-c, this is the only type of inheritance.

Private inheritance is also easy but pointless

A : private B means A is implemented by B. However, that's pointless because that means A should contain B instead. Ownership means less coupling with no disadvantage.

Then we have protected inheritance.

Can anyone explain to me what the hell is that for? Some says it's an "as a relationship". I am still not very clear on that.

Does anyone has some sample cases where people uses protected inheritance in good pattern (and conscience) for actual productive use?

user4951
  • 32,206
  • 53
  • 172
  • 282
  • I may be wrong, but I think I read in one of _the_ golden C++ books (don't remember which one exactly), that `protected` inheritance is (almost?) nonsense and (almost?) never used. Like "reserved for future usage". But, again, I may lie :) – Kiril Kirov Nov 28 '13 at 09:10
  • [this answer](http://stackoverflow.com/a/1374362/2513200) by Johannes Schaub actually found a use case. It still calls it "Rarely useful." – Hulk Nov 28 '13 at 09:13
  • Or try [this one](http://stackoverflow.com/questions/374399/why-do-we-actually-need-private-or-protected-inheritance-in-c/374423#374423) – neutrino Nov 28 '13 at 09:15
  • And private inheritance is also almost useless because it should be ownership instead. – user4951 Nov 28 '13 at 09:17
  • 1
    @JimThio Except when ownership doesn't work. (Mixins would be a good example.) More generally, private inheritance is also a pragmatic solution when you want to expose part (but not all) of the interface of the base class; `using` is a lot simpler than forwarding functions. – James Kanze Nov 28 '13 at 09:20
  • @JimThio: private inheritance *is* ownership. – n. m. could be an AI Nov 28 '13 at 09:22
  • You can check this [link](http://www.gotw.ca/publications/mill06.htm) – benipalj Nov 28 '13 at 09:23

2 Answers2

21

Private inheritance is also easy but pointless

A : private B means A is implemented by B. However, that's pointless because that means A should contain B instead. Ownership means less coupling with no disadvantage.

That you might not see reasons for private inheritance does not mean it's pointless. There are several cases where private inheritance has its reasons. You are right in that at a first glance, private inheritance means has-a relationships just like aggregation, and that private inheritance has a (slightly) tighter coupling.

Reasons for favouring private inheritance over aggretations could be some of the following:

  • With private inheritance you inherit typedefs as well. In some cases (e.g. traits classes) inheriting privatly is just the alternative to re-typedef tons of typedefs in the base class.
  • In seldom cases you have to initialize a member before a "real" (i.e. public) base class. The only way to achieve that is by making that member a private base class inherited before the public base.
  • Some times you need access to protected members of a member. If you can't change the member class itself, you have to use private inheritance to get access to them.
  • If a member has no data members of its own, it still occupies space. Making it a private base class enables the empty base class optimization, diminuishing the size of your class' objects.
  • for even more points, see James' comments below

These reasons are obviously technical reasons, some might even say "hacks". However, such reasons exist, so private inheritance is not completely pointless. It is just not "pure OO style" - but C++ isn't a pure OO language either.

The reason for protected inheritance are quite simple once you understood the ones for private inheritance:

If you have reasons to inherit something privately and want to make those benefits (i.e. that would-be member of your class or the typedefs) accessible to derived classes, use protected inheritance. Obviously, private inheritance should be used scarcely, and protected inheritance even more so.

zipson
  • 53
  • 1
  • 7
Arne Mertz
  • 24,171
  • 3
  • 51
  • 90
  • 1
    It's worth pointing out that the original inheritance, in say Smalltalk, corresponds closer to private inheritance than to public in C++. Inheritance was originally designed to support shared implementation, _not_ shared interface. – James Kanze Nov 28 '13 at 09:22
  • 5
    And of course, you've missed what is probably the most common reason for private inheritance: the base class provides a customizable implementation, using the template method pattern, and you have to override its virtual functions. – James Kanze Nov 28 '13 at 09:24
  • 6
    Not to mention that sometimes, the fact that you implement a particular interface is an implementation detail for your clients. The fact that a GUI widget is a GUI event listener only concerns the widget itself (and where it enrols for the events). – James Kanze Nov 28 '13 at 09:25
  • @JamesKanze thanks for the additional points. I fear the support of shared implementation is not seen as a good reason for inheritance these days, as people tend to be poisoned by the "everything has to be pure OO" mindset. – Arne Mertz Nov 28 '13 at 09:26
  • Re "pure OO": as I mentioned above, in the minds of the inventor of the term (and one of the authors of Smalltalk), inheritance was only inheritance of implementation. The first language to use the OO label used duck typing, so there was no need for inheritance of interface. (See Python, today, for an example of how this works.) – James Kanze Nov 28 '13 at 09:33
  • @JamesKanze I know. There were and are many languages that enable OO programming the way the inventor of the term thought of OO. However there is a huge crowd of programmers who think everything has to be an Object, implement some Interface and be part of at least one Design Pattern, free functions in C++ are evil, and laguages like Python are not really OO, because they are not *pure*. I have seen OO programs in C, yet those zealots want everything to be more like Java. – Arne Mertz Nov 28 '13 at 09:57
  • I used pure functions a lot. They are the most flexible. – user4951 Nov 28 '13 at 10:13
  • @JimThio so do I. But for example Java does not even allow them, instead they must be canned as static member functions into some class. Some people adopt it as a coding style for C++ as well because they think it's "more OO". – Arne Mertz Nov 29 '13 at 07:23
6

The main motivation for protected inheritance is orthogonality. in all other contexts, you have three different access controls: private, protected and public. Why should inheritance be different? In practice, one could argue that there is no need or use for protected access in general. That may be overstating the case, but it is certain that a lot less is protected than private or public.

Also, private inheritance is not at all pointless, and in fact, corresponds to the original use of inheritance. As soon as the base class doing the implementing uses virtual functions which the derived class has to overload, containment cannot be used.

James Kanze
  • 150,581
  • 18
  • 184
  • 329
  • 1
    I once used a protected virtual base to manage command-line arguments for the various passes of a linker; each pass was handled by its own type, and the linker type was derived privately from each of the pass types. In hindsight I'm not sure whether this was a good design or just showing off. – Pete Becker Nov 28 '13 at 14:22