In message <ea3f115.0408230600.e9f9915@posting.google.com>, Simon Turner
<s_j_turner@yahoo.co.uk> writes[color=blue]
>Richard Herring <junk@[127.0.0.1]> wrote:
>[color=green]
>> In message <426691c0.0408222158.1f00e1fd@posting.google.com >, ralpe
>> <ralph.peterson@gmx.net> writes[color=darkred]
>> >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?
>> >[/color][/color]
>
><snip>
>[color=green][color=darkred]
>> >
>> >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?[/color][/color]
>
>It can indeed.
>[color=green]
>>
>> 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.[/color]
>
>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[/color]
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