逐成员副本,按位副本,浅副本和深副本有什么区别? [英] What is the difference between memberwise copy, bitwise copy, shallow copy and deep copy?

查看:44
本文介绍了逐成员副本,按位副本,浅副本和深副本有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用谷歌搜索了这些术语,但我仍然感到困惑.

I have googled these terms but I am still confused.

有人说成员副本是深层副本,按位副本是浅层副本,但有人说不是.

Some people said that memberwise copy is deep copy and the bitwise copy is shallow copy but someone said it is not.

谁能向我解释默认副本构造函数和用户定义的副本构造函数使用哪种类型的副本?

Can anyone explain to me that which type of copy of the default copy constructor and the user-defined copy constructor use?

推荐答案

成员级复制

是当您访问每个成员并显式复制它时,调用其复制构造函数.它通常等同于深层复制.这是复制事物的正确而正确的方法.相反是按位复制,这是一个hack,请参见下文.

Is when you visit each member and explicitly copy it, invoking its copy constructor. It is usually tantamount to deep-copy. It is the right and proper way of copying things. The opposite is bit-wise copy, which is a hack, see below.

按位复制

是浅表副本的一种特定形式.在这种情况下,您只需使用 memcpy()或类似方法将源类的位复制到目标类.构造函数不会被调用,因此您倾向于得到一个看起来很正常的类,但是一旦开始使用它,事情就会以可怕的方式开始崩溃.这与成员级复制相反,它是一种快速而肮脏的技巧,当我们知道没有要调用的构造函数且没有要复制的内部结构时,有时可以使用它.有关此问题可能引起什么的讨论,请参见以下问答: C ++按位复制与按成员复制?

Is a specific form of shallow copy. It is when you simply copy the bits of the source class to the target class, using memcpy() or something similar. Constructors are not invoked, so you tend to get a class which appears to be all right but things start breaking in horrible ways as soon as you start using it. This is the opposite of member-wise copy, and is a quick and dirty hack that can sometimes be used when we know that there are no constructors to be invoked and no internal structures to be duplicated. For a discussion of what may go wrong with this, see this Q&A: C++ bitwise vs memberwise copying?

浅拷贝

是指仅复制对象的直接成员,而不复制它们指向的任何结构.这是您按位复制时得到的.

Refers to copying just the immediate members of an object, without duplicating whatever structures are pointed by them. It is what you get when you do a bit-wise copy.

(请注意,没有影子副本"之类的东西.我的意思是,文件系统中有这种事,但这可能不是您想的.)

深拷贝

不仅指复制对象的直接成员,还指复制它们所指向的任何结构.这是您进行成员级复制时通常得到的.

Refers to not only copying the immediate members of an object, but also duplicating whatever structures are pointed by them. It is what you normally get when you do member-wise copy.

总结:

有两类:

  • 浅拷贝
  • 深层复制

然后,有两种广泛使用的技术:

Then, there are two widely used techniques:

  • 按位复制(一种浅复制)
  • 成员级副本(深层副本的一种形式)

关于某人说了什么,而某人说了什么的传闻:按位复制肯定总是浅表复制.成员级副本通常是深层副本,但是您当然可以对其进行伪造,因此您可能会认为您正在制作深层副本,而实际上却并非如此.适当的成员级复制依赖于适当的复制构造函数.

As for the hear-say about someone who said something and someone who said something else: bit-wise copy is definitely always shallow copy. Member-wise copy is usually deep copy, but you may of course foul it up, so you may be thinking that you are making a deep copy while in fact you are not. Proper member-wise copy relies on having proper copy constructors.

最后:

如果已知对象是普通可复制的,则默认复制构造函数将按位进行复制,否则,将按成员进行复制.但是,编译器并不总是具有足够的信息来执行每个成员的正确副本.例如,通过复制指针而不是通过指向对象复制来复制指针.这就是为什么当对象不可复制时,通常不应该依赖编译器为您提供默认复制构造函数的原因.

The default copy constructor will do a bit-wise copy if the object is known to be trivially copyable, or a member-wise copy if not. However, the compiler does not always have enough information to perform a proper copy of each member. For example, a pointer is copied by making a copy of the pointer, not by making a copy of the pointed object. That's why you should generally not rely on the compiler providing you with a default copy constructor when your object is not trivially copyable.

用户提供的构造函数可以执行用户喜欢的任何类型的复制.希望用户能够明智地选择并进行成员复制.

A user-supplied constructor may do whatever type of copy the user likes. Hopefully, the user will choose wisely and do a member-wise copy.

这篇关于逐成员副本,按位副本,浅副本和深副本有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆