Deep copy and shallow copy in C++

 

The copy constructor is automatically called when an object of a custom class type that has already been initialized is used to initialize another newly constructed object. That is, when the object of the class needs to be copied, the copy constructor will be called. The copy constructor is called when:

(1) An object is passed into the function body by value 

(2) An object is returned from a function by passing it by value 

(3) An object needs to be initialized by another object.

If you do not explicitly declare a copy constructor in the class, then the compiler will automatically generate a default copy constructor that performs bitwise copying between objects. Bit copy, also known as shallow copy, will be described later.

Custom copy constructor is a good programming style, it can prevent the compiler from forming a default copy constructor and improve the efficiency of source code.

 


Shallow and deep copies

  In some cases, the member variables in the class need to dynamically open up heap memory. If a bit copy is implemented, that is, the value in the object is completely copied to another object, such as A=B. At this time, if a member variable pointer in B has already applied for memory, that member variable in A also points to the same piece of memory. This is a problem: when B releases the memory (such as: destructing), the pointer in A is a wild pointer, and an operation error occurs.

  Deep copy and shallow copy can be simply understood as: if a class has resources, when the object of this class is copied, the resources are re-allocated. This process is a deep copy. On the contrary, if there is no reallocation of resources, it is a shallow copy.

 


To summarize the basic concepts and knowledge you need to know about deep copy and shallow copy:

(1) When is the copy function used?

  a. An object is passed into the function body by value; 

  b. An object is returned from a function by passing by value;

  c. An object needs to be initialized by another object.

If you do not explicitly declare a copy constructor in the class, then the compiler will automatically generate a default copy constructor that performs bitwise copying between objects. bit copy aka shallow copy

(2) Should the copy function be customized?

(3) What is deep copy? What is a shallow copy? The difference between the two?

Custom copy constructor is a good programming style, it can prevent the compiler from forming a default copy constructor and improve the efficiency of source code.

Deep If a class has resources, when the object of this class is copied, the resources are reallocated, and this process is a deep copy. On the contrary, if there is no reallocation of resources, it is a shallow copy.

(4) Is a deep copy or a shallow copy better?

If a bit copy is implemented, that is, the value in the object is completely copied to another object, such as A=B. At this time, if a member variable pointer in B has already applied for memory, that member variable in A also points to the same piece of memory. This is a problem: when B releases the memory (such as: destructing), the pointer in A is a wild pointer, and an operation error occurs.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324109254&siteId=291194637