Boost logo

Boost Users :

Subject: Re: [Boost-users] [shared_ptr] Is it possible for a shared_ptr to hand over it ownership
From: Alan M. Carroll (amc_at_[hidden])
Date: 2008-12-27 15:17:38


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).

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 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