In message <>, Simon Turner
<> writes
>Richard Herring <junk@[127.0.0.1]> wrote:
>
>> In message < >, ralpe
>> <> writes
>> >Hi,
>> >
>> >I have a question regarding boost::shared_ptr. I want to let
>> >shared_ptr take care of my objects' lifetimes, so I change
>> >all my function signatures so that they accept shared_ptrs
>> >instead of raw pointers.
>> >
>> >I wonder how to deal with cases where a this pointer is
>> >passed as an argument. Is it ok to write something like the
>> >following?
>> >
>
><snip>
>
>> >
>> >I'm asking because shared_ptr is non-intrusive and I fear
>> >that the above code could create a second counter for an
>> >object that is already reference counted.
>> >
>> >Any comments?
>
>It can indeed.
>
>>
>> You could have a boost::weak_pointer member initialised from this, and
>> then use boost::make_shared to create a shared_ptr from it to pass to
>> add_to_collection. Using the weak_ptr will ensure that all shared_ptrs
>> created from it will share the same reference count.
>
>This can be automated by deriving your classes from
>boost::enable_shared_from_this.
>
>See:
> http://www.boost.org/libs/smart_ptr/...from_this.html
Better still! (enable_... is about two versions of Boost more recent
than my compiler can cope with, so I haven't yet been able to use it
myself

)
--
Richard Herring