Re: private and protected inheritance
From: Doug Harrison [MVP] (dsh_at_mvps.org)
Date: 11/06/04
- Next message: Jochen Kalmbach: "Re: Win32 CS semaphore use by run time library in VC 6.0"
- Previous message: Larry Brasfield: "Re: Win32 CS semaphore use by run time library in VC 6.0"
- In reply to: Carl Daniel [VC++ MVP]: "Re: private and protected inheritance"
- Messages sorted by: [ date ] [ thread ]
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++
- Next message: Jochen Kalmbach: "Re: Win32 CS semaphore use by run time library in VC 6.0"
- Previous message: Larry Brasfield: "Re: Win32 CS semaphore use by run time library in VC 6.0"
- In reply to: Carl Daniel [VC++ MVP]: "Re: private and protected inheritance"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|