shallow-copy

What is the difference between being shallowly and deeply equal? How is this applied to caching?

我与影子孤独终老i 提交于 2019-11-30 14:22:37
Found the following in my notes, but I am unable to make sense of it: Primitive type wrapper classes implement caching for a limited number of values. This guarantees that a limited number of deeply equal wrapper objects are also shallowly equal: If o1.equals( o2 ) then o1 == o2 . For example, new Integer( 0 ) == new Integer( 0 ) . In general this does not always work. For example, new Integer( 666 ) == new Integer( 666 ) may not hold. The reason for caching is that it saves memory. In general caching works for “small” primitive values. I don't understand what is meant by this, or what the

Changing list elements in shallow copy

时间秒杀一切 提交于 2019-11-30 09:08:28
问题 I have one question about list shallow copy. In both examples, I modified one element of the list, but in example 1, list b changed, while in example 2, list d is not changed. I am confused since in both examples, I modified an element of the list. What's the difference? Example 1: a=[1,2,[3,5],4] b=list(a) a[1]=0 print(a) # [1, 0, [3, 5], 4] print(b) # [1, 2, [3, 5], 4] Example 2: c=[1,2,[3,5],4] d=list(c) c[2][0]=0 print(c) # [1, 2, [0, 5], 4] print(d) # [1, 2, [0, 5], 4] 回答1: A shallow

Is clone() in java shallow copy?

僤鯓⒐⒋嵵緔 提交于 2019-11-30 08:58:43
Is clone() in java a shallow copy? Eventually this gets to the clone() method of Object (the uppermost class), which creates a new instance of the same class as the object and copies all the fields to the new instance (a "shallow copy"). I read this from wikipedia . I don't understand why it is a shallow copy. clone() will create a new instance with all fields. Is this just a deep copy? confused. Need some explanation for me. The default Object.clone() is indeed a shallow copy. However, it's designed to throw a CloneNotSupportedException unless your object implements Cloneable . And when you

VB.NET, Is Object Returned by Reference from Function

我只是一个虾纸丫 提交于 2019-11-30 05:09:29
问题 This should be a fairly common question, but I haven't found a straightforward answer anywhere. If I instantiate an object within a function in VB.NET and return it, does it return it by reference or by value. IE - should I be worried about performance if I write something like this: Public Function ret_obj_func() As big_object Dim ret_obj As New big_obj(<lots of stuff>) Return ret_obj End Function If I call this function from somewhere else, will it instantiate the object in the ret_obj and

Why are objects automatically passed by reference?

左心房为你撑大大i 提交于 2019-11-30 03:15:09
问题 I have a general question about deep- and shallow-copy in the context of the pass-by-reference- and pass-by-value-concept of C#: In C# it is a requirement to explicitly create methods that accept pointers/references to be able to pass such to the method. However, at least objects passed as parameters to methods/constructors are behaving differently from the rest. It seems they are always passed by reference if no extra cloning is done as described here: http://zetcode.com/lang/csharp/oopii/.

Changing list elements in shallow copy

余生长醉 提交于 2019-11-29 12:37:25
I have one question about list shallow copy. In both examples, I modified one element of the list, but in example 1, list b changed, while in example 2, list d is not changed. I am confused since in both examples, I modified an element of the list. What's the difference? Example 1: a=[1,2,[3,5],4] b=list(a) a[1]=0 print(a) # [1, 0, [3, 5], 4] print(b) # [1, 2, [3, 5], 4] Example 2: c=[1,2,[3,5],4] d=list(c) c[2][0]=0 print(c) # [1, 2, [0, 5], 4] print(d) # [1, 2, [0, 5], 4] A shallow copy means that you get a new list but the elements are the same. So both lists have the same first element,

Default assignment operator= in c++ is a shallow copy?

荒凉一梦 提交于 2019-11-29 10:35:52
问题 Just a simple quick question which I couldn't find a solid answer to anywhere else. Is the default operator= just a shallow copy of all the class' members on the right hand side? Class foo { public: int a, b, c; }; foo f1, f2; ... f1 = f2; would be identical to: f1.a = f2.a; f1.b = f2.b; f1.c = f2.c; This seems to be true when I test it but I need to be sure I'm not missing some specific case. 回答1: I'd say, default operator= is a copy . It copies each member. The distinction between a shallow

Shallow copy of a Map in Java

我只是一个虾纸丫 提交于 2019-11-28 17:20:49
As I understand it, there are a couple of ways (maybe others as well) to create a shallow copy of a Map in Java: Map<String, Object> data = new HashMap<String, Object>(); Map<String, Object> shallowCopy; // first way shallowCopy = new HashMap<String, Object>(data); // second way shallowCopy = (Map<String, Object>) ((HashMap<String, Object>) data).clone(); Is one way preferred over the other, and if so, why? One thing worth mentioning is that the second way gives an "Unchecked Cast" warning. So you have to add @SuppressWarnings("unchecked") to get around it, which is a little irritating (see

When should I pass or return a struct by value?

好久不见. 提交于 2019-11-28 05:42:31
A struct can be either passed/returned by value or passed/returned by reference (via a pointer) in C. The general consensus seems to be that the former can be applied to small structs without penalty in most cases. See Is there any case for which returning a structure directly is good practice? and Are there any downsides to passing structs by value in C, rather than passing a pointer? And that avoiding a dereference can be beneficial from both a speed and clarity perspective. But what counts as small ? I think we can all agree that this is a small struct: struct Point { int x, y; }; That we

How would you improve this shallow copying class?

杀马特。学长 韩版系。学妹 提交于 2019-11-28 04:29:37
问题 I've written a class with a single static method that copies property values from one object to another. It doesn't care what type each object is, only that they have identical properties. It does what I need, so I'm not engineering it further, but what improvements would you make? Here's the code: public class ShallowCopy { public static void Copy<From, To>(From from, To to) where To : class where From : class { Type toType = to.GetType(); foreach (var propertyInfo in from.GetType()