C++ 深拷贝与浅拷贝

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2657810/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 00:15:15  来源:igfitidea点击:

Deep copy vs Shallow Copy

c++clone

提问by Ankur

Possible Duplicate:
What is the difference between a deep copy and a shallow copy?

可能的重复:
深拷贝和浅拷贝有什么区别?

What is the difference between deep and shallow copy. What type of a copy does a copy constructor do?

深拷贝和浅拷贝有什么区别。复制构造函数做什么类型的副本?

回答by stakx - no longer contributing

Shallow copy:

浅拷贝:

Some members of the copy may reference the same objects as the original:

副本的某些成员可能引用与原始对象相同的对象:

class X
{
private:
    int i;
    int *pi;
public:
    X()
        : pi(new int)
    { }
    X(const X& copy)   // <-- copy ctor
        : i(copy.i), pi(copy.pi)
    { }
};

Here, the pimember of the original and copied Xobject will both point to the same int.

在这里,pi原始X对象和复制对象的成员都将指向同一个int.



Deep copy:

深拷贝:

All members of the original are cloned (recursively, if necessary). There are no shared objects:

原始的所有成员都被克隆(递归,如有必要)。没有共享对象:

class X
{
private:
    int i;
    int *pi;
public:
    X()
        : pi(new int)
    { }
    X(const X& copy)   // <-- copy ctor
        : i(copy.i), pi(new int(*copy.pi))  // <-- note this line in particular!
    { }
};

Here, the pimember of the original and copied Xobject will point to different intobjects, but both of these have the same value.

在这里,pi原始X对象和复制对象的成员将指向不同的int对象,但它们具有相同的值。



The default copy constructor (which is automatically provided if you don't provide one yourself) creates only shallow copies.

默认的复制构造函数(如果您自己不提供,则会自动提供)仅创建浅拷贝。

Correction:Several comments below have correctly pointed out that it is wrong to say that the default copy constructor alwaysperforms a shallow copy (or a deep copy, for that matter). Whether a type's copy constructor creates a shallow copy, or deep copy, or something in-between the two, depends on the combination of each member's copy behaviour; a member's type's copy constructor can be made to do whatever it wants, after all.

更正:下面的几条评论正确地指出,说默认复制构造函数总是执行浅复制(或深复制,就此而言)是错误的。一个类型的拷贝构造函数是创建浅拷贝还是深拷贝,还是介于两者之间的东西,取决于每个成员的拷贝行为的组合;毕竟,成员类型的复制构造函数可以做任何它想做的事情。

Here's what section 12.8, paragraph 8 of the 1998 C++ standard says about the above code examples:

以下是 1998 C++ 标准的第 12.8 节第 8 段对上述代码示例的描述:

The implicitly defined copy constructor for class Xperforms a memberwise copy of its subobjects. [...] Each subobject is copied in the manner appropriate to its type: [...] [I]f the subobject is of scalar type, the builtin assignment operator is used.

类的隐式定义复制构造函数X执行其子对象的成员复制。[...] 每个子对象都以适合其类型的方式复制: [...] [I] 如果子对象是标量类型,则使用内置赋值运算符。

回答by cletus

The quintessential example of this is an array of pointers to structs or objects (that are mutable).

这方面的典型例子是指向结构或对象(可变的)的指针数组。

A shallow copycopies the array and maintains references to the original objects.

一个浅拷贝复制阵列,并保持原来的对象的引用。

A deep copywill copy (clone) the objects too so they bear no relation to the original. Implicit in this is that the object themselves are deep copied. This is where it gets hard because there's no real way to know if something was deep copied or not.

一个深拷贝,使他们没有承担相对于原始将复制(克隆)的对象了。这暗示着对象本身是深度复制的。这就是它变得困难的地方,因为没有真正的方法知道某些东西是否被深度复制。

The copy constructor is used to initilize 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 Constructors and destructors.

为了阅读带有完整示例和解释的详细信息,您可以参阅文章构造函数和析构函数

The default copy constructor is shallow. You can make your own copy constructors deep or shallow, as appropriate. See C++ Notes: OOP: Copy Constructors.

默认的复制构造函数是浅的。您可以根据需要使自己的复制构造函数变深或变浅。请参阅C++ 注释:OOP:复制构造函数

回答by n535

Deep copy literally performs a deep copy. It means, that if your class has some fields that are references, their values will be copied, not references themselves. If, for example you have two instances of a class, A & B with fields of reference type, and perform a deep copy, changing a value of that field in A won't affect a value in B. And vise-versa. Things are different with shallow copy, because only references are copied, therefore, changing this field in a copied object would affect the original object.

深拷贝字面上执行深拷贝。这意味着,如果您的类有一些作为引用的字段,它们的值将被复制,而不是引用本身。例如,如果您有一个类的两个实例,A 和 B 具有引用类型的字段,并执行深层复制,则更改 A 中该字段的值不会影响 B 中的值。反之亦然。浅拷贝就不一样了,因为只拷贝了引用,所以在拷贝对象中改变这个字段会影响到原始对象。

What type of a copy does a copy constructor does?

复制构造函数的副本类型是什么?

It is implementation - dependent. This means that there are no strict rules about that, you can implement it like a deep copy or shallow copy, however as far as i know it is a common practice to implement a deep copy in a copy constructor. A default copy constructor performs a shallow copy though.

它依赖于实现。这意味着对此没有严格的规则,您可以像深拷贝或浅拷贝一样实现它,但是据我所知,在复制构造函数中实现深拷贝是一种常见的做法。但是,默认的复制构造函数执行浅复制。