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