Boost logo

Boost Users :

Subject: Re: [Boost-users] [shared_ptr] Is it possible for a shared_ptr to
From: Max (loadcom_at_[hidden])
Date: 2008-12-28 19:36:53


Thank you all for your information.
Of course, new elegant solution or just simple hints are still welcome.

B/Rgds
Max

----- Original Message -----
From: Andy Wiese <andyw_at_[hidden]>
To: boost-users_at_[hidden]
Subject: Re: [Boost-users] [shared_ptr] Is it possible for a shared_ptr to hand over it ownership
Date: 2008-12-28 04:54:08


On Dec 27, 2008, at 2:17 PM, Alan M. Carroll wrote:

> I don't see why you would ever want to do that. Why not hand over
> ownership to another shared_ptr? Why can B in your example not be of
> type shared_ptr<>? Then you can hand over ownership by
>
> b = p1;
> p1 = 0;
>
> Or, if B must be a class, why not have a member that's a shared_ptr
> and have getOwnership(shared_ptr& src) do
>
> _member = src;
> src = 0;
>
> The object can be explicitly destroyed by assigning 0 to _member.
>
> I would recommend against using auto_ptr, as it is just so _odd_. I
> used it once when Boost.scoped_ptr wasn't quite what I wanted, but
> that was a mistake (although I did find a nifty bug in the Microsoft
> compiler implemementation of auto_ptr).
>

I find auto_ptr's strict semantics to be useful in APIs because it
makes a clear statement about the lifecycle of an object. If some
function returns an auto_ptr, then you know right there that the
client has full authority over that objects lifecycle.

> At 06:57 AM 12/25/2008, Max wrote:
>> Suppose I have a shared_ptr which is the only owner of the pointee.
>> I hope there is a way to hand over its ownership to another object
>> without rendering the pointee being deleted.
>>
>> Or, if the answer is no, is there an alternative class that has
>> this feature?
>>
>> The use case is like this:
>>
>> B b;
>> {
>> shared_ptr p1(A()), p2(A());
>>
>> b.GetOwnership(p1.HandOver());
>> // now p1 is empty
>> }
>> // p2 is deleted here
>>
>> // but the A obj p1 used to be pointing to
>> // has been handed over to b and is still
>> // alive here
>>
>> b.DestroyA();
>> // the A obj holded by b is destroyed by b
>> // explicitly
>
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users

_______________________________________________
Boost-users mailing list
Boost-users_at_[hidden]
http://lists.boost.org/mailman/listinfo.cgi/boost-users

-------------------------------------------------------------------
ÐÂÀ˿ռ䡪¡ªÓëÅóÓÑ¿ªÐÄ·ÖÏíÍøÂçÐÂÉú»î£¡(http://space.sina.com.cn/ )


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