什么时候是浅层复制(而不是深层复制)? [英] When is a shallow copy desirable (instead of a deep copy)?

查看:118
本文介绍了什么时候是浅层复制(而不是深层复制)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以举例说明需要浅拷贝的情况吗?



请注意,在某些情况下,浅拷贝和深拷贝是相同的。这可能发生在对象对其任何子变量没有所有权时;也就是说,所有子变量都聚合。我想看到一个对象从它拥有的变量组成的示例,并且仍然希望复制

:我不在乎使用哪种语言的例子。我从C ++ / Java / C#的角度来问这个问题,虽然我认为复制是一个语言不可知的概念。

解决方案

p>当拥有的变量是不可变类型时,浅拷贝就足够了。深度复制是可能的,但只会导致额外的内存使用。


Can someone give an example of a situation where a shallow copy is needed?

Note that in some situations, shallow copy and deep copy are the same. This can happen when the object has no ownership over any of its subvariables; that is, all subvariables are aggregated. I'd like to see examples where the an object is composed from variables which it owns, and it still is desirable to copy them shallowly.

Remark: I don't care in which language the examples are given. I'm asking this question from a C++/Java/C# point of view, although I think copying is a language agnostic concept.

解决方案

When the owned variables are immutable types, a shallow copy is sufficient. A deep copy is possible, but would only result in additional memory use.

这篇关于什么时候是浅层复制(而不是深层复制)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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