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.
A class that requires deep copies generally needs:
A constructor to either make an initial allocation or set the pointer to NULL.
A destructor to delete the dynamically allocated memory.
A copy constructor to make a copy of the dynamically allocated memory.
An overloaded assignment operator to make a copy of the dynamically allocated memory.
发表评论
-
const 与static 关键词在c 和 c++中的作用
2012-08-11 11:49 967一.C语言中的const ... -
error LNK2005 and error LNK2019
2012-08-09 23:08 1097error LNK2005 and error LNK2019 ... -
LIBCD.lib(crt0dat.obj) : error LNK2005: _exit already defined in msvcrtd.lib(MSV
2012-08-07 09:20 1385LIBCD.lib(crt0dat.obj) : error ... -
在VS2010下配置VC++ Directories
2012-08-07 08:29 10451:可以 在“class View” 面板 -> 右 ... -
零星笔记,待整理
2010-11-01 13:00 478//用 getline 函数从输入读取整行内容。然后为 ... -
VC: error LNK2019:unresolved external symbol *** referenced in function ***的解决方案
2010-10-30 21:13 2106当头文件中声明了一个函数,但是在相应的源文件中却没有对该函数进 ... -
stdcall cdecl fastcall thiscall naked call的具体含义
2010-10-30 18:54 959在C语言中,假设我们有 ... -
#define总结 (网上资料汇集)
2010-10-30 08:06 7161. 定义简单的常数:定义常量,便于修改(切不可在后面加上分号 ... -
(zz)编译与使用Qt4,Visual Studio Express 2008
2010-10-14 22:35 1375一、Qt版本 我现在用的是4.5.2。好像前段时间4.6出来 ... -
strcat 内部实现
2010-09-21 00:43 1798char * strcat(char * dest, ... -
reinterpret_cast的本质(转)
2010-09-14 23:15 1613先看看下面的代码: #include <iostr ... -
C++ 经典书籍
2010-09-14 16:18 1684下载地址: http://www.51cnnet.net/di ... -
内存对齐的规则以及作用
2010-09-14 00:14 687首先由一个程序引入话 ... -
[转]C++ reinterpret_cast,const_cast等 显式类型转换总结
2010-09-13 23:23 1012#include<iostream.h> ... -
C++提供了四种新的类型强制
2010-09-13 23:13 684static_cast const_cast reinte ... -
C++著名程序库的比较和学习经验
2010-09-13 19:30 8241、C++各大有名库的介绍——C++标准库 2、C++各大有名 ... -
c++ library and resource List
2010-09-13 18:56 8971,前言 无数次听 ... -
C++中操作符operator的两种用法
2010-09-12 02:23 11171.operator overloading C++可能通过o ... -
两种自动类型转换:构造函数与operator
2010-09-12 02:23 889如果有两个对象:one, two, 要想把one转换成为two ... -
C++中的类拷贝构造函数和模板拷贝构造函数
2010-09-11 20:53 3002类(包括模板类)构造函数是真实的构造函数;然而模板构造函数,其 ...
相关推荐
浅拷贝和深拷贝深入理解(shallow copy VS deep copy) 本文重点讨论引用类型变量的拷贝机制和实现
1.Difference between shallow copy and deep copy? 浅复制和深复制的区别? 答案:浅层复制:只复制指向对象的指针,而不复制引用对象本身。 深层复制:复制引用对象本身。 意思就是说我有个A对象,复制一份...
在Python中,浅拷贝(shallow copy)和深拷贝(deep copy)是用于创建对象副本的两种常见方式。它们可以应用于不同类型的对象,如列表、字典、集合等。下面是对浅拷贝和深拷贝的描述: 浅拷贝:浅拷贝是创建一个新...
3.9. * copy ——Shallow and deep copy operations 18 4. 函数 20 4.1. 什么是函数? 20 4.2. 函数的用处 20 4.3. 函数中的参数传递 20 4.4. 默认参数 20 4.5. 不定长参数 21 4.6. 递归函数 21 4.7. 拓展知识:...
浅复制(shallow copy):在浅复制操作时,对于被复制对象的每一层都是指针复制。 深复制(one-level-deep copy):在深复制操作时,对于被复制对象,至少有一层是深复制。 完全复制(real-deep copy):在完全复制操作...
Chapter 10 Example of Shallow Copy vs. Deep Copy, page 459 Solution to Programming Project 10.5, page 469 Chapter 11 Avoiding Multiple Defi nitions with #ifndef, page 484 Solution to Programming ...
标题中的4个术语,对应的英文分别是:shallow copy(注意,不是shadow copy)、deep copy、pass by value、pass by reference(或pass by address)。传址和传引用是一回事。 一门编程语言的核心是数据结构,粗略来...
在开发过程中我们经常会遇到对象拷贝的问题,下面我们分别讨论赋值操作、对象拷贝、以及浅拷贝(Shallow copy)与深拷贝(Deep copy)的区别与各自的实现方式。 一、不同对象的赋值操作 Objective-C中有两类对象,...
浅拷贝(deep copy)与深拷贝(shallow copy) 我们首先来看复制构造函数涉及的两个概念:浅拷贝与深拷贝。假设有两个对象:A与B,它们是同类型的,下面分析B=A时浅拷贝与深拷贝行为。 浅拷贝: 浅拷贝...
对象拷贝(Object Copy)就是将一个对象的属性拷贝到另一个有着相同类类型的对象中去。在程序中拷贝对象是很常见的,主要...Java中有三种类型的对象拷贝:浅拷贝(Shallow Copy)、深拷贝(Deep Copy)、延迟拷贝(Lazy Copy)
* Implement PHP __clone to create a deep clone, not just a shallow copy. */ public function __clone() { $vars = get_object_vars($this); foreach ($vars as $key => $value) { if (is_object($...
<param name="bRecurseIntoSubClasses">A boolean flag that when true performs a deep copy of the parameters, otherwise a shallow copy is performed.</param> <returns>An object that represents the ...
The copy and mutableCopy Methods 413 Shallow Versus Deep Copying 416 Implementing the <NSCopying> Protocol 418 Copying Objects in Setter and Getter Methods Exercises 423 19 Archiving 425 Archiving ...
2.10 Finding the Deep Index of an Item in an Embedded Sequence 2.11 Showing Off Quicksort in Three Lines 2.12 Sorting Objects Using SQL's ORDER BY Syntax 3. Text 3.1 Processing a String One ...
Python参考手册,官方正式版参考手册,chm版。以下摘取部分内容:Navigation index modules | next | Python » 3.6.5 Documentation » Python Documentation contents What’s New in Python ...