
-----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users- bounces@lists.boost.org] On Behalf Of James Hughes Sent: Monday, March 20, 2006 11:29 AM To: 'boost-users@lists.boost.org' 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.