《Cracking the Coding Interview》——第13章:C和C++——题目4

2014-04-25 19:50

题目:深拷贝和浅拷贝有什么区别?如何应用?

解法:深拷贝传值,浅拷贝传引用。java里对此做了限制,而C++里面用起来更自由。大结构不宜传值,因为拷贝过程效率低。

代码:

 1 // 13.4 What's deep copy and shallow copy? Expain their appications in different cases.
 2 // Answer:
 3 //    deep copy:
 4 //        1. pass by value, often used on small data type, built-in data types.
 5 //        2. data is usually persistent, can be seriailzed directly.
 6 //        3. the data to be deep-copied usually represents objects and entities.
 7 //    shallow copy:
 8 //        1. pass by reference or pointer, often used on large struct or class.
 9 //        2. data is volatile, serialization must be done with the help of persistent data type.
10 //        3. the data to be shallow-copied usually represents relations and mappings between entities.
11 int main()
12 {
13     return 0;
14 }
原文地址:https://www.cnblogs.com/zhuli19901106/p/3689898.html