How to deep copy the full object behind boost :: shared_ptr <T> to shared_ptr pointing to a new location?

How to copy a complete object in boost::shared_ptr<T>: are there memcopy options (just create a memory clone), or will we create a copy constructor?

+5
source share
3 answers

You need a copy constructor or operator=one that will perform a deep copy.

boost::shared_ptrcannot know your structure of objects to do this for you. The operation "memory cloning" also does not work.

Of course, this is only for objects that require an explicitly defined copy constructor / operator=, while "trivial" ones are small copies.

+1
source

, .

, , .

+1

You should create a copy constructor and write something like:

ptr.reset(new T);
*ptr  = *(otherObject.ptr);

to deep copy the pointer.

0
source

All Articles