Re: [Boost-users] [shared_ptr] Is it possible for a shared_ptr to
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
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@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users ------------------------------------------------------------------- 新浪空间——与朋友开心分享网络新生活!(http://space.sina.com.cn/ )
participants (1)
-
Max