What is the shallow and deep copying of objects in C++?
A shallow copy of object copies all of the member field values. This works well if the fields are values, and pointer will be copied, but the memory it points to will not be copied. So the objects will point to the same memory. The default copy constructor and assignment operator make shallow copies.
A deep copying will copy all fields, and make copies of dynamically allocated memory pointed to by the fields. You must write a copy constructor yourself.If an object has pointers to dynamically allocated memory, and the dynamically allocated memory needs to be copied when the original object is copied, then a deep copy is required.
A shallow copy of object copies all of the member field values. This works well if the fields are values, and pointer will be copied, but the memory it points to will not be copied. So the objects will point to the same memory. The default copy constructor and assignment operator make shallow copies.
A deep copying will copy all fields, and make copies of dynamically allocated memory pointed to by the fields. You must write a copy constructor yourself.If an object has pointers to dynamically allocated memory, and the dynamically allocated memory needs to be copied when the original object is copied, then a deep copy is required.