Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the practical use of protected inheritance?

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?

like image 208
user4951 Avatar asked Nov 28 '13 09:11

user4951


People also ask

What happens when a protected member is inherited as public?

Protected Inheritance − When deriving from a protected base class, public and protected members of the base class become protected members of the derived class. Private Inheritance − When deriving from a private base class, public and protected members of the base class become private members of the derived class.

Do protected members become private when inherited?

With protected inheritance, the public and protected members become protected, and private members stay inaccessible. With private inheritance, all members from the base class are inherited as private. This means private members are inaccessible, and protected and public members become private.

Can protected can be inherited?

If one wishes to access a protected modifier outside a package, then inheritance is needed to be applied.

What is purpose of private inheritance in C++?

The private -inheritance variant allows access to the protected members of the base class. The private -inheritance variant allows Car to override Engine 's virtual functions.


2 Answers

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 it's 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.

like image 58
Arne Mertz Avatar answered Oct 07 '22 14:10

Arne Mertz


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.

like image 27
James Kanze Avatar answered Oct 07 '22 15:10

James Kanze