Re: Smart Pointer showing deep copy semantics
On Dec 25, 12:43 pm, ariji...@gmail.com 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:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34