Smart Pointer showing deep copy semantics

Discussion in 'C++' started by arijit79@gmail.com, Dec 25, 2008.

  1. Guest

    There are so many types of smart pointers around like shared_ptr,
    unique_ptr, auto_ptr, each with their own set of benefits.

    I wonder why isn't there a smart pointer which implements deep copy
    semantics.

    That would be helpful in following cases:

    class X {
    ...
    };

    class Y {
    X * x;
    ...

    public:
    const X * getX() const; // X may not always be present, so might
    return NULL sometimes.
    ...
    Y(const Y & y); // I will have to implement deep copy semantics
    here:-(
    Y & operator=(const Y & y); // I will have to implement deep copy
    semantics here :-(
    ...
    };

    For such a case, I would love to have wrapped around my data member
    pointer 'x' with some kind of smart ptr like "std::deep_ptr<X> x;" so
    that I get the followng benefit:
    1) I don't have to bother about manually implementing deep copy in
    copy constructor and aissignment operator. And yes, all exception
    safety things are also taken care of.
    2) I will still continue to have the liberty that getX() may sometimes
    return NULL i.e. X may not always be present as per the real world
    object I am modelling.

    The only way (I can think of) for achiving both these benefits
    together is to implement a deep copy smart ptr and use it.

    I could do that, but I was was wondering, if people felt the need for
    all other kind of smart ptrs and put it in the standard/boost library,
    why was this kind of a deep_ptr missing?

    In other words, am i missing something? Or am i using C++ in a not so
    well recommended way?

    Thanks,
    Arijit
     
    , Dec 25, 2008
    #1
    1. Advertising

  2. Kai-Uwe Bux Guest

    wrote:

    > There are so many types of smart pointers around like shared_ptr,
    > unique_ptr, auto_ptr, each with their own set of benefits.
    >
    > I wonder why isn't there a smart pointer which implements deep copy
    > semantics.

    [snip]

    There is. Just google the archives for copy_ptr or clone_ptr and you will
    find plenty.


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Dec 25, 2008
    #2
    1. Advertising

  3. James Kanze Guest

    On Dec 25, 12:43 pm, wrote:
    > There are so many types of smart pointers around like
    > shared_ptr, unique_ptr, auto_ptr, each with their own set of
    > benefits.


    > I wonder why isn't there a smart pointer which implements deep
    > copy semantics.


    There are. I use them from time to time. Most of the time, it
    isn't what you want: values support copy, and you copy them, and
    don't have pointers to them. Entities don't support copy. But
    there are exceptions, e.g. the letter envelope idiom.

    > For such a case, I would love to have wrapped around my data
    > member pointer 'x' with some kind of smart ptr like
    > "std::deep_ptr<X> x;" so that I get the followng benefit:
    > 1) I don't have to bother about manually implementing deep
    > copy in copy constructor and aissignment operator. And yes,
    > all exception safety things are also taken care of.
    > 2) I will still continue to have the liberty that getX() may
    > sometimes return NULL i.e. X may not always be present as per
    > the real world object I am modelling.


    > The only way (I can think of) for achiving both these benefits
    > together is to implement a deep copy smart ptr and use it.


    > I could do that, but I was was wondering, if people felt the
    > need for all other kind of smart ptrs and put it in the
    > standard/boost library, why was this kind of a deep_ptr
    > missing?


    There are any number of possible smart pointers. Don't be
    mislead by Boost: on the whole, the smart pointers it contains
    are the least useful ones (although scoped_ptr can be useful at
    times).

    --
    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, Dec 25, 2008
    #3
    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. VisionSet
    Replies:
    8
    Views:
    4,928
    Tris Orendorff
    Apr 29, 2004
  2. Alex
    Replies:
    2
    Views:
    1,254
  3. Replies:
    1
    Views:
    550
    Victor Bazarov
    Jun 24, 2005
  4. Replies:
    26
    Views:
    2,140
    Roland Pibinger
    Sep 1, 2006
  5. Replies:
    11
    Views:
    1,008
    Nindi
    Nov 14, 2006
Loading...

Share This Page