c - 浅/深复制术语是否适用于没有引用的对象?

免责声明

这篇文章是关于术语“浅拷贝”和“深拷贝”的正确用法,特别是在谈论复制一个不包含任何引用的对象时。这个问题并不意味着(也不应该)基于意见,除非真的没有关于这个话题的共识。我已将此问题标记为 C,但它可能与语言无关,除非在该上下文中这些术语的含义针对特定语言有明确定义,但对于其他语言则不然。

前言

术语“浅拷贝”和“深拷贝”通常在复制带有引用的对象时使用,以指定拷贝是否完整(独立于原始拷贝) .

但是,我也看到在复制没有引用的对象时使用了这个术语,这两个术语的意思完全相同,没有必要区分。到目前为止,我还没有找到一个简明的定义来涵盖这些术语的这种特殊用法。

  • Stack Overflow 上给出的定义(在标签 shallow-copy 和 deep-copy 中):

    A shallow copy contains a link (address in memory) to the original variable. Changes on shallow copies are reflected on origin object.

    A deep copy duplicates the object or variable being pointed to so that the destination (the object being assigned to) receives its own local copy.

    根据这些定义,没有引用的对象副本将是深拷贝。

  • Wikipedia 上的定义(在文章 Object copying 中):

    One method of copying an object is the shallow copy. In that case a new object B is created, and the fields values of A are copied over to B. This is also known as a field-by-field copy, field-for-field copy, or field copy. If the field value is a reference to an object (e.g., a memory address) it copies the reference, hence referring to the same object as A does, and if the field value is a primitive type it copies the value of the primitive type. In languages without primitive types (where everything is an object), all fields of the copy B are references to the same objects as the fields of original A. The referenced objects are thus shared, so if one of these objects is modified (from A or B), the change is visible in the other. Shallow copies are simple and typically cheap, as they can be usually implemented by simply copying the bits exactly.

    An alternative is a deep copy, meaning that fields are dereferenced: rather than references to objects being copied, new copy objects are created for any referenced objects, and references to these placed in B. The result is different from the result a shallow copy gives in that the objects referenced by the copy B are distinct from those referenced by A, and independent. Deep copies are more expensive, due to needing to create additional objects, and can be substantially more complicated, due to references possibly forming a complicated graph.

    根据这些定义,没有引用的对象副本将是浅拷贝。

我觉得这两个词都不合适,因为“shallow-copy”意味着复制不完整,而“deep-copy”意味着复制需要某种特殊处理(或高成本)。 由于复制没有引用的对象既完整又不需要任何特殊处理,因此我认为不应使用这两个术语。然而,这篇文章不是关于我的想法,而是编程社区目前的共识(如果有的话)是什么。

问题

当我复制一个没有引用的对象时,会被认为是

  • 浅拷贝(因为不涉及引用)?
  • 深拷贝(因为目标对象独立于源对象)?
  • 两个?
  • 都没有?

部分深拷贝是否有一个好的术语,其中一些字段是浅拷贝而另一些是深拷贝?

最佳答案

当区别不适用时,就称之为“副本”。它不是浅拷贝,因为没有共享引用,也不是深拷贝,因为只复制结构中的值。

这个问题就像问石头是不是无神论者。当然,他们不是有神论者。但是有神论/无神论的区别真的适用于他们吗?有些秤只设计用于测量某些东西。

https://stackoverflow.com/questions/62956777/

相关文章:

list - 如何 List.sum 一个列表(也许是 float )?

amazon-web-services - 在 AWS 的子网中检索私有(private) IP 地

asp.net-mvc - 如何将 Azure Active Directory 身份验证添加到 R

r - 错误 : Must subset columns with a valid subscrip

vue.js - Nuxt : Open component/page as modal with

python - Pyspark - 根据一行中的条件过滤掉多行

python - Jupyter notebook 感叹号参数

python - 如何在 Python 中读取 SPSS aka (.sav)

python - 在循环中的每个字符后插入句点

r - 对于循环存储问题