复制构造函数/操作符/函数是否需要明确它实现的复制变体? [英] Does a copy constructor/operator/function need to make clear which copy variant it implements?

查看:16
本文介绍了复制构造函数/操作符/函数是否需要明确它实现的复制变体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

昨天我问了 一个关于在 C# 中复制对象的问题,大多数答案都集中在深拷贝浅拷贝之间的区别,以及应该明确给定拷贝构造函数(或运算符或函数)实现这两种拷贝变体中的哪一种.我觉得这很奇怪.

Yesterday I asked a question about copying objects in C#, and most answers focussed on the difference between deep copy and shallow copy, and the fact that it should be made clear which of both copy variants a given copy constructor (or operator, or function) implements. I find this odd.

我用 C++ 编写了很多软件,这是一种严重依赖复制的语言,而且我从不需要多个复制变体.我曾经使用过的唯一一种复制操作是我称之为足够深的复制"的操作.它执行以下操作:

I wrote a lot of software in C++, a language that heavily relies on copying, and I never ever needed multiple copy variants. The only kind of copy operation I ever used is the one I call "deep enough copy". It does the following:

  • 如果对象拥有成员变量的所有权(参见 composition),它被递归复制.
  • 如果对象对成员变量没有所有权(参见 aggregation),只复制链接.
  • In case the object has ownership over the member variable (cf. composition), it is copied recursively.
  • In case the object has no ownership over the member variable (cf. aggregation), only the link is copied.

现在,我的问题有三个:

Now, my question is threefold:

  • 1) 一个对象是否需要多个副本变体?
  • 2) 复制函数是否需要明确它实现的复制变体?
  • 3) 顺便说一句,我所说的足够深的副本"是否有更好的术语?我问了一个关于定义的 相关问题术语深拷贝".

推荐答案

深拷贝"和浅拷贝"之间的区别作为一个实现细节是有意义的,但允许它超出这个范围通常表明有缺陷的抽象也可能以其他方式表现出来.

The distinction between of "deep copy" versus "shallow copy" makes sense as an implementation detail, but allow it to leak beyond that generally indicates a flawed abstraction which will likely manifest itself in other ways as well.

如果一个对象 Foo 持有一个对象引用,纯粹是为了封装其中包含的对象的不可变方面,除了身份,那么 Foo 的正确副本code>Foo 可能包含引用的副本或对封装对象副本的引用.

If an object Foo holds an object reference purely for the purpose of encapsulating immutable aspects, other than identity, of the object contained therein, then a correct copy of Foo may either contain a duplicate of the reference or a reference to a duplicate of the encapsulated object.

如果一个对象 Foo 持有一个对象引用,纯粹是为了封装对象的可变和不可变方面除了身份,但永远不会引用该对象暴露于任何会改变它的东西,同样的情况也适用.

If an object Foo holds an object reference purely for the purpose of encapsulating mutable and immutable aspects of an object other than identity, but no reference to that object will ever be exposed to anything that would mutate it, the same situation applies.

如果一个对象 Foo 持有一个对象引用,纯粹是为了封装对象的可变和不可变方面除了身份,并且有问题的对象将如果发生变异,则 Foo 的正确副本必须包含对封装对象副本的引用.

If an object Foo holds an object reference purely for the purpose of encapsulating mutable and immutable aspects of an object other than identity, and the object in question is going to be mutated, then a correct copy of Foo must contain a reference to a duplicate of the encapsulated object.

如果一个对象 Foo 持有一个对象引用纯粹是为了封装对象的不可变方面包括身份,那么 Foo 的正确副本code> 必须包含 reference 的副本;它必须包含对重复对象的引用.

If an object Foo holds an object reference purely for the purpose of encapsulating immutable aspects of the object including identity, then a correct copy of Foo must contain a duplicate of the reference; it must NOT contain a reference to a duplicated object.

如果一个对象 Foo 持有一个对象引用以封装可变状态和对象标识,那么就不可能在其中生成 Foo 的正确副本隔离.Foo 的正确副本只能通过复制它所附加的整个对象集来生成.

If an object Foo holds an object reference for the purpose of encapsulating both mutable state and object identity, then it is not possible to produce a correct copy of Foo in isolation. A correct copy of Foo may only be produced by duplicating the entire set of objects to which it is attached.

只有在使用不完整的操作作为制作正确副本的步骤之一时才有意义地谈论浅拷贝".否则,只有一个正确的副本深度",由封装在对象引用中的状态类型控制.

The only time it makes sense to talk about a "shallow copy" is when an incomplete operation is used as one of the steps in making a correct copy. Otherwise, there is only one correct copy "depth", controlled by the type of state encapsulated in object references.

这篇关于复制构造函数/操作符/函数是否需要明确它实现的复制变体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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