c++-deep copy

Deep copy vs. shallow copy

  • Deep copy : open up new memory, independent objects, heap area
  • Shallow copy : shared memory, reference object, stack area
deep copy

Deep copy is a copy method that reallocates memory in the heap area and copies the contents of the object. This means that the original object and the new object are completely independent, and modifications to one object will not affect the other. Usually used to deal with dynamically allocated memory situations, such as data pointed to by pointers.

Shallow copy

Shallow copy is a copy method that simply copies the value or reference of an object. This means that the original object and the new object will share the same memory, and modifications to one object may affect the other object. Typically used to handle objects allocated on the stack, such as primitive data types or references to objects.

おすすめ

転載: blog.csdn.net/qq_43537701/article/details/133028456