深度复制操作是否以递归方式复制它不拥有的子变量? [英] Does a deep copy operation recursively copies subvariables which it doesn't own?

查看:87
本文介绍了深度复制操作是否以递归方式复制它不拥有的子变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出一个对象,该对象具有它不拥有的变量;也就是说,该变量由聚合而不是

Given an object that has a variable which it doesn't own; that is, the variable is composed by aggregation instead of composition. Will a deep copy operation copy the variable or only the link to it?

推荐答案

我喜欢您在深层复制的上下文中在组合和聚合之间的区别.

I like the distinction that you are making here between the role of composition and aggregation in the context of a deep copy.

我要与另一个答案相反:不,一个对象不应深度复制它不拥有的另一个对象.

I am going to go against the other answer and say: no, an object should not deep-copy another object that it doesn't own.

人们希望对象的深层副本(至少在最初)与原始副本相同.如果对原始副本不拥有的引用进行了深拷贝,那么这将成为新副本拥有者的问题.如果克隆拥有它,那么它将与原始对象不同.它将是一个类似于原始对象的对象,只是它拥有对其聚合成员之一的引用.这肯定会导致混乱.如果克隆不拥有它,那么谁拥有它?

One would expect a deep copy of an object to be (at least initially) identical to the original. If a deep copy were made of a reference that the original didn't own, then this leaves open the question of what owns the new copy. If the clone owns it, then it would not be identical to the original object. It would be an object like the original, except it owns the reference to one of its aggregated members. This would surely lead to chaos. If the clone doesn't own it, then who does?

这个所有权问题在非垃圾收集语言中尤其重要,但是即使使用垃圾收集器,它也会带来问题.例如,如果进行克隆以允许对对象进行未提交的更改,那么是否允许对该对象引用的另一个对象进行更改?如果不允许更改,则没有理由对其进行深度复制.如果允许更改,那么由于修改的对象无法控制此引用的对象,这些更改将如何提交?当然,可以为此设计一种机制,但这无疑意味着克隆的对象正在超越其职责,并且该程序将成为维护的噩梦.

This problem of ownership is especially important in non-garbage-collected languages, but it also creates problems even with a garbage collector. For example, if the clone is made to allow uncommitted changes to an object, are changes to be allowed on this other object that it references? If changes are not allowed, then there was no reason to deep-copy it. If changes are allowed, then how are those changes to be committed, since the object being modified doesn't control this referenced object? Sure, a mechanism for this could be contrived, but it would surly mean that the cloned object is overstepping its responsibilities, and the program would be a maintenance nightmare.

包括无主对象的深层复制操作也会导致无限(或至少过多)复制操作的问题.假设一个对象是集合的一部分,并且进一步假设该对象需要对该集合的引用.然后,对该对象进行简单的深层复制操作将创建该集合及其每个成员的新副本.即使假设我们避免了无限递归的问题,并且在所有新对象之间保持所有引用的一致性,对于大多数目的而言,它仍然是过多的,对于那些需要新集合的情况,这样做是否更有意义?为此目的要深深地复制收藏集本身,而不是其成员之一?

A deep copy operation that includes unowned objects also leads to problems of infinite (or at least excessive) copy operations. Suppose an object is part of a collection, and further suppose the object requires a reference to the collection. A naive deep-copy operation on that object would then create a new copy of the collection and each of its members. Even assuming that we avoid the problem of infinite recursion, and keep all the references consistent among this new set of objects, it is still excessive for most purposes, and for those cases where a new collection is desired, wouldn't it make more sense to deep-copy the collection itself, rather than one of its members, for this purpose?

我认为,如您所建议的,仅包含拥有对象的深层副本是大多数目的唯一明智的方法.

I think a deep-copy that only includes owned objects, as you suggest, is the only sane approach for most purposes.

这篇关于深度复制操作是否以递归方式复制它不拥有的子变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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