Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Practical Implementation of Private Inheritance

2 views
Skip to first unread message

sk.s...@gmail.com

unread,
Mar 21, 2007, 3:05:52 PM3/21/07
to
Hi All,

Can some body let me know where Private Inheritance is used in C++? I
am looking for some practical and Real time implementations NOT
conceptual ideas.

Warm Regards.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Ivan Novick

unread,
Mar 22, 2007, 5:20:39 AM3/22/07
to
On Mar 21, 12:05 pm, sk.sma...@gmail.com wrote:
> Hi All,
>
> Can some body let me know where Private Inheritance is used in C++? I
> am looking for some practical and Real time implementations NOT
> conceptual ideas.
>
> Warm Regards.

Sorry its not a practical example, but private inheritance is for
inheriting implementation without inheriting interface. So you can
defined a new interface in the derived class but all the functionality
is inherited.

This concept can be applied in any N real-world applications.

Ivan Novick
http://wwww.0x4849.net

Vladimir Marko

unread,
Mar 22, 2007, 10:21:53 AM3/22/07
to
On 21 Mar, 19:05, sk.sma...@gmail.com wrote:
> Can some body let me know where Private Inheritance is used in C++? I
> am looking for some practical and Real time implementations NOT
> conceptual ideas.

For example when implementing some behaviour with objects using the
observer pattern. For example

class InstantMessageProtocol
: private SocketObserver // implementation detail
{
public:
InstantMessageProtocol(){
socket_ = new Socket(*this);
//< convert *this to SocketObserver&
}
SendMessage(const Recipient& recipient,const std::string&
message);
// ...

private:
Socket* socket_; // implementation detail
// Socket reports events via the following callbacks:
void SendComplete(int error_code); // from SocketObserver
void PacketReceived(); // from SocketObserver
// ...
};

Regards,
Vladimir Marko

Seungbeom Kim

unread,
Mar 22, 2007, 10:20:21 AM3/22/07
to
sk.s...@gmail.com wrote:
> Can some body let me know where Private Inheritance is used in C++? I
> am looking for some practical and Real time implementations NOT
> conceptual ideas.

See this message and thread from the past:
http://groups.google.com/group/comp.lang.c++.moderated/browse_frm/thread/35c34091a34298f8/f4b765eb77950dfa#f4b765eb77950dfa

--
Seungbeom Kim

James Kanze

unread,
Mar 22, 2007, 10:22:06 AM3/22/07
to
On Mar 21, 8:05 pm, sk.sma...@gmail.com wrote:

> Can some body let me know where Private Inheritance is used in C++? I
> am looking for some practical and Real time implementations NOT
> conceptual ideas.

The usual case is when one is inheriting implementation, but not
interface. There are numerous examples in my own work, for
example: AssocArrayOf<> from HashTableImpl, FixedLengthAllocator<>
from AbstractFixedLengthAllocator, almost all of my ostream and
istream classes from StreambufWrapper. The first two cases
could almost be considered a pattern: a template class inherits
from a non-template base class which contains the
type-independent code. In the last, I use virtual inheritance
to ensure that the concerned sub-object is constructed first,
before the base class I really want to inherit from.

I also have a lot of classes which inherit privately from
UncopiableObject. Arguably, this should be public inheritance,
since the fact that they inherit from this class has a
significant impact on their public interface. But
UncopiableObject doesn't have a virtual destructor, and I don't
want people upcasting pointers to it.

In my application code (not present at my site), I also
occasionally make use of mixins, and will often use private
inheritance there.

For a more detailed discussion, and yet more examples, you might
try Barton and Nackman. They're pretty rigorous about it.

--
James Kanze (GABI Software) email:james...@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

werasm

unread,
Mar 22, 2007, 12:49:42 PM3/22/07
to

sk.s...@gmail.com wrote:
> Hi All,
>
> Can some body let me know where Private Inheritance is used in C++? I
> am looking for some practical and Real time implementations NOT
> conceptual ideas.

For now I'll give a short (perhapse conceptual) answer. To give you my
examples may just take very long.

Typically, I find that private inheritance is required when one wants
to implement in terms of, whilst allowing for the possibility to
override virtual functions. A small example of this is the sending of
commands (from the command pattern) over queues.

The idea is that the destination can only use a partial interface. The
command is associated with the queue, and sends a base pointer to
itself over the queue (or server). The server cannot execute (or
envoke) interface not belonging to it (as it only has the base). The
source (that originally envoked the command) cannot call methods that
the server would call, as the derived inherits privately.

You see, therefore that private inheritance may be essential for
encapsulation. This type of encapsulation could probably be achieved
in other (less natural) ways.

Kind regards,

Werner

Jiang

unread,
Mar 23, 2007, 1:35:59 AM3/23/07
to

>
> Can some body let me know where Private Inheritance is used in C++? I
> am looking for some practical and Real time implementations NOT
> conceptual ideas.
>

Please check SGI STL containers implementation for real world usage
if possible. ( protected inheritance used but the idea is same)

The std::vector's implementation is a good start point in my mind.

-> http://www.sgi.com/tech/stl/stl_vector.h

BTW, did you really mean "real-time" implementation in your previous
post? That would be a different problem in my mind. :-)

ta0...@yahoo.com

unread,
Apr 1, 2007, 3:33:58 PM4/1/07
to
On Mar 21, 3:05 pm, sk.sma...@gmail.com wrote:
> Hi All,
>
> Can some body let me know where Private Inheritance is used in C++? I
> am looking for some practical and Real time implementations NOT
> conceptual ideas.

I have a lib which encapsulates global objects for protection across
an entire project. This lib provides access limitations to the public
interface of enclosed object "X" by storing it as a private data
member and allowing certain objects to access it via the encapsulating
object's own public interface. This involves mutexes and viewing
counters, etc.

One way for a user to encapsulate their objects is to implement an
abstract interface of their own via an encapsulation class from the
lib. The final object inherits the encapsulation class, which in turn
inherits the abstract interface being implemented (via a template
argument.)

Because the access functions of the encapsulating object set the mutex
before allowing access, the implemented functions cannot check the
mutex before modifying the protected object. This leaves them
vulnerable so they must be protected from the outside world. This is
done with private inheritance in the encapsulating class.

By the final class inheriting the interface privately, those functions
can't be accessed directly, but the encapsulating object can convert
the final object to the abstract class on it's own terms (when the
mutex is set.)

http://capsule-ta0kira.sourceforge.net/
NOTE: I've been unable to decide between private and protected, but
the idea is the same. The only reason I've chosen protected for the
time being is so that the final class can pass a constructor argument
if needed, but I will probably switch back to private next release.

Kevin P. Barry

0 new messages