`

deep copy & shallow copy

    博客分类:
  • c++
 
阅读更多

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.

分享到:
评论

相关推荐

    浅拷贝和深拷贝深入理解(shallow copy VS deep copy)

    浅拷贝和深拷贝深入理解(shallow copy VS deep copy) 本文重点讨论引用类型变量的拷贝机制和实现

    ios面试题锦集chm版

    1.Difference between shallow copy and deep copy?
浅复制和深复制的区别?
 答案:浅层复制:只复制指向对象的指针,而不复制引用对象本身。
深层复制:复制引用对象本身。
意思就是说我有个A对象,复制一份...

    python浅拷贝、深拷贝

    在Python中,浅拷贝(shallow copy)和深拷贝(deep copy)是用于创建对象副本的两种常见方式。它们可以应用于不同类型的对象,如列表、字典、集合等。下面是对浅拷贝和深拷贝的描述: 浅拷贝:浅拷贝是创建一个新...

    python中阶基础

    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. 拓展知识:...

    详解iOS的深浅拷贝

    浅复制(shallow copy):在浅复制操作时,对于被复制对象的每一层都是指针复制。 深复制(one-level-deep copy):在深复制操作时,对于被复制对象,至少有一层是深复制。 完全复制(real-deep copy):在完全复制操作...

    Absolute C++

    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 ...

    javascript中的变量是传值还是传址的?

    标题中的4个术语,对应的英文分别是:shallow copy(注意,不是shadow copy)、deep copy、pass by value、pass by reference(或pass by address)。传址和传引用是一回事。 一门编程语言的核心是数据结构,粗略来...

    IOS ObjectiveC中的赋值与对象拷贝

    在开发过程中我们经常会遇到对象拷贝的问题,下面我们分别讨论赋值操作、对象拷贝、以及浅拷贝(Shallow copy)与深拷贝(Deep copy)的区别与各自的实现方式。 一、不同对象的赋值操作 Objective-C中有两类对象,...

    编译器角度看C++复制构造函数

     浅拷贝(deep copy)与深拷贝(shallow copy)  我们首先来看复制构造函数涉及的两个概念:浅拷贝与深拷贝。假设有两个对象:A与B,它们是同类型的,下面分析B=A时浅拷贝与深拷贝行为。  浅拷贝:  浅拷贝...

    Java中的深拷贝和浅拷贝介绍

    对象拷贝(Object Copy)就是将一个对象的属性拷贝到另一个有着相同类类型的对象中去。在程序中拷贝对象是很常见的,主要...Java中有三种类型的对象拷贝:浅拷贝(Shallow Copy)、深拷贝(Deep Copy)、延迟拷贝(Lazy Copy)

    好用的phpExcel

    * 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($...

    NETCFSERUP

    <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 ...

    Programming in Objective-C 4th Edition

    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 ...

    Python Cookbook英文版

    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 ...

    python3.6.5参考手册 chm

    Python参考手册,官方正式版参考手册,chm版。以下摘取部分内容:Navigation index modules | next | Python » 3.6.5 Documentation » Python Documentation contents What’s New in Python ...

Global site tag (gtag.js) - Google Analytics