Deep copy and shallow copy
来源:互联网 发布:c语言编写的程序 编辑:程序博客网 时间:2024/05/28 05:19
点击打开链接
明确两个基本概念:浅拷贝(Shallow Copy)与深拷贝(Deep Copy)
1.浅拷贝(Shallow Copy)=(bitwise copy:位元逐一复制,按位拷贝)指的是拷贝对象而不拷贝该对象包含的对象,对它的嵌套的对象,仅拷贝其句柄。2.深拷贝(Deep Copy)指在拷贝对象的时候连同拷贝它所包含的对象
A shallow copy of an object copies all of the member field values. This works well if the fields are values, but may not be what you want for fields that point to dynamically allocated memory. The pointer will be copied. but the memory it points to will not be copied -- the field in both the original object and the copy will then point to the same dynamically allocated memory, which is not usually what you want. The default copy constructor and assignment operator make shallow copies.
A deep copy copies all fields, and makes copies of dynamically allocated memory pointed to by the fields. To make a deep copy, you must write a copy constructor and overload the assignment operator, otherwise the copy will point to the original, with disasterous consequences.
Deep copies need ...
If an object has pointers to dynamically allocated memory, and the dynamically allocated memory needs to be copied when the original object is copied, then a deep copy is required.
在c#的结构中自动实现了一种“memberwise“拷贝,也被称作“shallow copy”。object基类提供了一个protected方法,MemberwiseClone,来实现一个类成员的memberwise copy。
如果类中的成员有一个或多个是引用类型,使用shallow copy 并不足够好。这将产生对相同数据的两个引用,而不是两个独立的数据副本。要拷贝数据自身而不是它的引用,你需要执行一个 “deep copy”.深度拷贝能够在语言级或类库级别上提供,c++通过语言级别的copy constructor来实现。在c#中,deep copy是通过.net framework中的特定接口,ICloneable,提供。为了对一个类实现的deep copy,你应当让这个类实现这个接口.
0 0
- Deep Copy and Shallow Copy
- Shallow Copy and Deep Copy
- shallow copy and deep copy
- Deep copy and shallow copy
- deep copy and shallow copy
- Java Clone, Shallow Copy and Deep Copy
- Deep copy & Shallow copy
- Shallow copy & Deep copy
- objective-c shallow- and deep-copy
- Python: Shallow and deep copy operations
- [Python]Shallow and Deep copy operation
- deep copy vs shallow copy
- Deep Copy VS Shallow Copy
- Shallow Copy与Deep Copy
- shallow copy与deep copy
- shallow & deep copy
- Shallow vs. deep copy
- 深拷贝与浅拷贝(Deep Copy and Shallow Copy)
- jquery避免单双击事件冲突写法
- flexdashboard: Easy interactive dashboards for R
- 权限问题导致Nginx 403 Forbidden错误的解决方法
- jQuery 操作元素
- 93.国际化简述及使用步骤
- Deep copy and shallow copy
- log4j 使用
- ESP8266 学习笔记
- JDK源码分析之String篇
- 跟奥巴马一起编程
- 有图有案例!125个提升网页可用性的优化小技巧(一)
- Spring Mvc与struts2区别
- 欢迎使用CSDN-markdown编辑器
- JSP九大内置对象及四个作用域详解