|
Boost Users : |
From: Nat Goodspeed (ngoodspeed_at_[hidden])
Date: 2006-03-20 13:30:40
> -----Original Message-----
> From: boost-users-bounces_at_[hidden] [mailto:boost-users-
> bounces_at_[hidden]] On Behalf Of James Hughes
> Sent: Monday, March 20, 2006 11:29 AM
> To: 'boost-users_at_[hidden]'
> Subject: Re: [Boost-users] enable_shared_from_this - or an
alternative..
>
> > >I have a couple of classes that I want to self register
> > themselves with a
> > >monitoring class. To do this they must pass a shared_ptr of
> > themselves to
> > >the monitor register function.
> > >
> > >Therefore I need to make a shared_ptr form the this pointer, in the
> > >constructor.
[Nat] We have a similar need: the base class registers in a central
registry; we have many subclasses of the base class.
We use dumb base* pointers for this purpose. We can get away with that
because, just as the base-class constructor registers each instance, so
the base-class virtual destructor unregisters the instance.
Indeed, for our purposes, we don't WANT smart pointers in the registry.
If the last smart-pointer reference to a heap object goes away, we don't
want the reference in the registry to make the instance immortal. In
that case, the smart pointer to the heap object decrements the count to
0, the object is destroyed, and its destructor unregisters the instance.
A side benefit is that we don't have to care whether the instance is
static, auto or heap. Nor do we need factory functions.
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net