What is the difference between a deep copy and a s

2018-12-31 00:00发布

What is the difference between a deep copy and a shallow copy?

30条回答
无与为乐者.
2楼-- · 2018-12-31 00:12

The copy constructor is used to initialize the new object with the previously created object of the same class. By default compiler wrote a shallow copy. Shallow copy works fine when dynamic memory allocation is not involved because when dynamic memory allocation is involved then both objects will points towards the same memory location in a heap, Therefore to remove this problem we wrote deep copy so both objects have their own copy of attributes in a memory. In order to read the details with complete examples and explanations you could see the article C++ constructors.

查看更多
浅入江南
3楼-- · 2018-12-31 00:14

'ShallowCopy' points to the same location in memory as 'Source' does. 'DeepCopy' points to a different location in memory, but the contents are the same.

查看更多
查无此人
4楼-- · 2018-12-31 00:15

To add more to other answers,

  • a Shallow Copy of an object performs copy by value for value types based properties, and copy by reference for reference types based properties.
  • a Deep Copy of an object performs copy by value for value types based properties, as well as copy by value for reference types based properties deep in the hierarchy (of reference types)
查看更多
心情的温度
5楼-- · 2018-12-31 00:16

I would like to give example rather than the formal definition.

var originalObject = { 
    a : 1, 
    b : 2, 
    c : 3,
};

This code shows a shallow copy:

var copyObject1 = originalObject;

console.log(copyObject1.a);         // it will print 1 
console.log(originalObject.a);       // it will also print 1 
copyObject1.a = 4; 
console.log(copyObject1.a);           //now it will print 4 
console.log(originalObject.a);       // now it will also print 4

var copyObject2 = Object.assign({}, originalObject);

console.log(copyObject2.a);        // it will print 1 
console.log(originalObject.a);      // it will also print 1 
copyObject2.a = 4; 
console.log(copyObject2.a);        // now it will print 4 
console.log(originalObject.a);      // now it will print 1

This code shows a deep copy:

var copyObject2 = Object.assign({}, originalObject);

console.log(copyObject2.a);        // it will print 1 
console.log(originalObject.a);      // it will also print 1 
copyObject2.a = 4; 
console.log(copyObject2.a);        // now it will print 4 
console.log(originalObject.a);      // !! now it will print 1 !!
查看更多
十年一品温如言
6楼-- · 2018-12-31 00:16

Imagine there are two arrays called arr1 and arr2.

arr1 = arr2;   //shallow copy
arr1 = arr2.clone(); //deep copy
查看更多
明月照影归
7楼-- · 2018-12-31 00:17

Shallow copying is creating a new object and then copying the non-static fields of the current object to the new object. If a field is a value type --> a bit-by-bit copy of the field is performed; for a reference type --> the reference is copied but the referred object is not; therefore the original object and its clone refer to the same object.

Deep copy is creating a new object and then copying the nonstatic fields of the current object to the new object. If a field is a value type --> a bit-by-bit copy of the field is performed. If a field is a reference type --> a new copy of the referred object is performed. The classes to be cloned must be flagged as [Serializable].

查看更多
登录 后发表回答