Reputation: 800
How to copy full object behind boost::shared_ptr<T>
: are there memcopy options (just create memory clone), or we shall create copy constructor?
Upvotes: 5
Views: 5814
Reputation: 21408
If you know the exact type of the object, then you should use a copy constructor or copy assignment operator.
If the objects is an instance of a class in an inheritance hierarchy, and you do not know the exact type of the object, then you should use a virtual clone function.
Upvotes: 1
Reputation: 38173
You need a copy constructor or an operator=
that will perform the deep copy.
boost::shared_ptr
cannot know your object's structure to do this for you. Neither can a "memory clone" operation.
Of course, this is only for objects, that need an explicitly defined copy constructor / operator=
and the "trivial" ones make a shallow copy.
Upvotes: 1
Reputation: 76240
You should create a copy constructor and write something like:
ptr.reset(new T);
*ptr = *(otherObject.ptr);
in order to deep copy the pointer.
Upvotes: 0