问题描述
能有人给的,其中需要一个浅拷贝情况的一个例子?
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.
备注:我不关心使用何种语言的例子给出。我问这个问题,从C的角度++ / Java的/ C#点,虽然我觉得抄袭是一种语言无关的概念。
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.
这篇关于如果是浅拷贝理想的(而不是深拷贝)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!