Smart Pointer showing deep copy semantics

A

arijit79

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
 
K

Kai-Uwe Bux

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
 
J

James Kanze

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top