boost eqiv of std::auto_ptr

Discussion in 'C++' started by Christopher, Apr 12, 2009.

  1. Christopher

    Christopher Guest

    If I have a factory that is allocating objects and returning them from
    one of its methods, what kind of smart pointer should I be using?

    I would normally use an std::auto_ptr, but was told that it is not as
    "safe" as a boost pointer. So, I looked at boost and thought a scoped
    pointer was a good fit, but it cannot be copied. My factory has a
    number of these things that it allocates and stored in a vector until
    the are retrieved.

    So, should I be using a boost shared_ptr then? Even though only one
    class will be dealing with the allocated object at a time?
     
    Christopher, Apr 12, 2009
    #1
    1. Advertising

  2. Christopher

    James Kanze Guest

    On Apr 13, 12:32 am, Christopher <> wrote:
    > If I have a factory that is allocating objects and returning
    > them from one of its methods, what kind of smart pointer
    > should I be using?


    It depends on the objects, but either a raw pointer or
    std::auto_ptr would seem most appropriate.

    > I would normally use an std::auto_ptr, but was told that it is
    > not as "safe" as a boost pointer.


    It's safer than boost::shared_ptr, at any rate. There may be
    special cases where a factory should return a boost::shared_ptr,
    but they are very, very exceptional.

    > So, I looked at boost and thought a scoped pointer was a good
    > fit, but it cannot be copied. My factory has a number of these
    > things that it allocates and stored in a vector until the are
    > retrieved.


    I'm not too sure how that works, but note that you cannot have
    an std::auto_ptr in a vector (or any other standard container).
    If the objects *are* actually in a vector, you probably can't
    (and don't want) to use a smart pointer for them at all---the
    vector manages their lifetime.

    > So, should I be using a boost shared_ptr then? Even though
    > only one class will be dealing with the allocated object at a
    > time?


    As a basic rule, a factory should not return a
    boost::shared_ptr, because this constrains the client code too
    much. If boost::shared_ptr is appropriate in the client code,
    it has a constructor from std::auto_ptr for this case; if it's
    not, however. Once a boost::shared_ptr has gotten control of an
    object, it can't let go---there's no way of changing the
    ownership policy.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Apr 13, 2009
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ctick
    Replies:
    4
    Views:
    8,986
    Richard Herring
    Jun 22, 2004
  2. BekTek
    Replies:
    9
    Views:
    852
    Jonathan Turkanis
    Nov 29, 2004
  3. Siemel Naran

    auto_ptr<Derived> to auto_ptr<Base>

    Siemel Naran, Jan 10, 2005, in forum: C++
    Replies:
    2
    Views:
    1,561
    Dave Rahardja
    Jan 11, 2005
  4. Misiu
    Replies:
    3
    Views:
    2,394
    Misiu
    Jan 31, 2007
  5. Sousuke
    Replies:
    9
    Views:
    1,157
    Bart van Ingen Schenau
    Mar 16, 2010
Loading...

Share This Page