Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does boost::ptr_vector deep copy the underlying objects?

Tags:

c++

ptr_vector is copy constructible and copy assignable. How can it deep copy the underlying objects when it doesn't know their concrete types?

like image 428
Billy ONeal Avatar asked Aug 07 '10 02:08

Billy ONeal


People also ask

How do you deep copy an object?

Copy an Object With Object.assign() was the most popular way to deep copy an object. Object. assign() will copy everything into the new object, including any functions. Mutating the copied object also doesn't affect the original object.

Does copy constructor create deep copy?

The default copy constructor makes a member-wise copy, and whether a deep or shallow copy is made of a member depends entirely on the behavior of members. struct Person { string firstName, lastName; } - the default copy constructor makes a deep copy.

What is deep copy in OOP?

Deep copy is a process in which the copying process occurs recursively. It means first constructing a new collection object and then recursively populating it with copies of the child objects found in the original. In case of deep copy, a copy of object is copied in other object.


1 Answers

The boost::ptr_vector container has an optional template parameter, CloneAllocator, that defines the cloning policy. The default allocator is the heap_clone_allocator, which simply invokes the copy constructor to clone an object.

The Clone Allocator is used as a way to add a layer of indirection around the cloning. For example, it allows you to provide a custom allocator that correctly handles cloning of a noncopyable type.

You can find more information in the Boost Pointer Containers Library documentation, which explains the Clonable and Clone Allocator concepts.

like image 96
James McNellis Avatar answered Sep 28 '22 01:09

James McNellis