[C++] Deep copy and shallow copy ① (Introduction to the concepts of deep copy and shallow copy | Comparison of shallow copy and deep copy | Usage scenarios of shallow copy and deep copy)





1. Introduction to the concepts of deep copy and shallow copy




1. Shallow copy


Shallow copy:

  • Shallow copy assignment of surface member variables: When copying an object, only the top-level members of the object are copied, that is, only the object itself and the object member variables are copied, and the sub-variables in the member variables are not copied;
  • When the member variables are pointers or references: If the member variables in the object are pointers or references pointing to other objects, when the object is copied, the pointers or references themselves are copied, and the pointers or references still point to the original memory;
  • Impact of shallow copy: After using shallow copy to copy the original object, modifications to the copied object may affect the original object; because the two objects hold the same pointer/reference;

Shallow copy scenario: Suppose there is a class containing string member variables. When using shallow copy to copy the object, the string pointer will be copied, and the two objects hold the same pointer variable value; if the string member variables of the copied object are If modified, the string member variables of the original object will also be modified;


When using shallow copy, do not modify the memory space pointed by the pointer/reference, otherwise it will cause various unknown problems;


2. Deep copy


Deep copy:

  • Deep copy assignment of surface member variables: When copying an object, the top-level members and sub-members of the object are copied. Not only the object itself and the object member variables are copied, but also the sub-variables in the member variables are copied;
  • When the member variables are pointers or references: If the member variables in the object are pointers or references to other objects, when the object is copied , the pointers or references themselves will not be copied, but the memory data pointed to by the pointers or references will be copied. to the new memory space and reallocate a pointer pointing to the new memory space;
  • Impact of deep copy: After using deep copy to copy the original object, modifications to the copied object will not affect the original object; because the two objects hold different pointers/references, pointing to different memory spaces;

Deep copy scenario: Suppose there is a class containing string member variables. When using deep copy to copy the object, the memory address pointed to by the string pointer will be copied. The two objects hold different pointers pointing to different memory spaces; if the copy is The string member variables of the object are modified, and the string member variables of the original object will not be modified;


3. Comparison between shallow copy and deep copy


There is no advantage or disadvantage between deep copy and shallow copy, they have different application scenarios;

  • Deep copy is more secure and is a complete data copy. The data is a complete backup, but the corresponding copy performance will decrease and occupy more memory/CPU resources;
  • Shallow copy lacks security, but has high performance and high execution efficiency;

Select a specific copy plan based on the characteristics of deep copy and shallow copy, as well as the application scenarios of the developed program;


4. Usage scenarios of shallow copy and deep copy


Shallow copy applicable scenarios:

  • Member variables are not references/pointers: member variables in an object are not references or pointers to other objects;
  • The reference/pointer type of member variables is copyable: the member variable reference in the object or the object type pointed to by the pointer can be copied;
  • The copy constructor is simple: The implementation of the object's copy constructor and copy assignment operator is relatively simple, and there is no need to deal with the copy of the object's internal sub-objects;
  • The copied object is independent: modifications to the copied object will not affect the original object, and the independence of the object does not need to be guaranteed;

Deep copy applicable scenarios:

  • Member variables are references/pointers: when the object contains references or pointers to other objects, and the objects pointed to by these references or pointers are of different types or cannot be copied;
  • The copy constructor is complex: the implementation of the object's copy constructor and copy assignment operator needs to handle the copy of the object's internal sub-objects;
  • The copied object has no independence: when modifications to the copied object will affect the original object, a deep copy must be used;

Guess you like

Origin blog.csdn.net/han1202012/article/details/132917152