Boost logo

Boost Users :

Subject: Re: [Boost-users] [shared_ptr] Is it possible for a shared_ptr to hand over it ownership
From: Emil Dotchevski (emildotchevski_at_[hidden])
Date: 2008-12-25 10:33:42


On Thu, Dec 25, 2008 at 4:57 AM, Max <loadcom_at_[hidden]> 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
> }

That's not possible because in general shared_ptr can use custom
deleters and/or aliasing which B can't deal with, unless it's
shared_ptr itself. :)

Consider:

boost::shared_ptr<FILE>
open_file( char const * name, char const * mode )
{
  if( FILE * f=fopen(name,mode) )
    return boost::shared_ptr<FILE>(f,fclose);
  else
    boost::throw_exception(file_open_error());
}

Emil Dotchevski
Reverge Studios, Inc.
http://www.revergestudios.com/reblog/index.php?n=ReCode


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