Briefly describe deep copy and shallow copy in Java, shallow copy and deep copy in C++

  The clone method in Java is a shallow copy. If you want to implement a deep copy, you need to inherit the cloneable interface and override the clone method.

  Primitive data types directly copy the value over. But the object type, which is a reference, points to a real object, then there are two ways to copy it: directly copy the reference value in the source object to the field of the new object, or according to the character pointed to in the original object A string object creates a new identical object, assigning a reference to the new object to the fields of the newly copied object.

  When does C++ use the copy constructor? Use shallow copy in C++ to pay attention to the generation of wild pointers. The default copy constructor is a shallow copy.

  1. An object is passed into the function body by value.

  2. An object is returned from a function by pass-by-value.

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

Guess you like

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