Re: private and protected inheritance

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Doug Harrison [MVP] (dsh_at_mvps.org)
Date: 11/06/04


Date: Sat, 06 Nov 2004 11:59:16 -0600

Carl Daniel [VC++ MVP] wrote:

>private inheritance is used to prevent code outside the class from
>performing a derived-to-base pointer conversion. Private inheritance is
>functionally equivalent to embedding an instance of the base class as a
>private member, and many experts recommend using embedding instead of
>private inheritance. The differences are purely syntactic.

There is one significant difference. Derived classes can override virtual
functions belonging to private bases. So in that sense, a private base is
not a mere implementation detail. I used to like private inheritance a lot
for designing classes that think of themselves as "ISA" WRT to their private
bases, but which provide a restricted interface to the outside world. In
particular, using-declarations are quicker to write than forwarding
functions. I still think there's some validity to that, but the overriding
issue, well, overrides it, and embedding should be used for would-be private
bases that have virtual functions.

In some cases, you don't know much about the base, and you might want to
take advantage of the empty-base optimization where possible without running
afoul of any virtual functions the base class has; Nathan Meyers wrote about
that here, which ISTR is where I first heard about the private base
overriding issue:

The "Empty Member" C++ Optimization
http://www.cantrip.org/emptyopt.html

-- 
Doug Harrison
Microsoft MVP - Visual C++


Relevant Pages

  • Re: adding key to DB object
    ... d> Why do you see using private attributes like ... d> $dbh->as preferable to embedding ... d> a dbh in your own object? ... Why would you use the latter method? ...
    (perl.dbi.users)
  • Re: Equivalent of dynamic_cast (downcast) for tagged types
    ... I am not a language lawyer to tell if the behavior (public overriding ... of a private primitive operation) is legal. ... It must be overriding because Adjust is a primitive operation. ... package Pak1 is ...
    (comp.lang.ada)
  • Re: Fans of Template Method with protected variable?
    ... >I've recently started using protected variables in the super class instead. ... >ask when you guys implement this pattern do you do it with private variables ... As a rule I don't like protected variables because the base class ... The cost is time. ...
    (comp.object)
  • Re: Derived private interface
    ... If the programmer doesn't understand the model of overriding in Ada, ... for a type that is simply private? ... You should not use any indicators in this case, as the type in question is ... Only derived types inherit anything, ...
    (comp.lang.ada)
  • Re: Accessing private member via subclass
    ... "Even though a derived class inherits the private members of a base ... But it's legal code (in C#, but not in Java) so it's worthy of academic discussion. ... However, and again in both languages, I don't expect an instance of T to behave like its base class S just because a base class private member is accessed in the lexical scope of S. This is not intuitive to me at all. ...
    (comp.lang.java.programmer)