shallow-copy

How to use both default and custom copy constructor in C++?

送分小仙女□ 提交于 2021-02-16 15:56:18
问题 I have a long class with a lot of data members. I want to write a copy constructor for it. But, if I write my own copy constructor, I lose access to the default copy constructor. I just want to repair a few pointers in my own copy constructor. So I want to have a shallow copy of the object which can be done by the default copy constructor. Is there a possibility to access the default copy constructor when I have my own copy constructor? 回答1: Wrap the things you don't want to change in a

Python list slice as shallow copy

廉价感情. 提交于 2021-02-11 06:20:04
问题 foo = [1, 2, 3] foo[:][0] = 5 foo doesn't change, also: import copy foo = [1, 2, 3] boo = copy.copy(foo) boo[0] = 5 Again, foo[0] stays the same. Why? The shallow copy creates new list, but shouldn't boo[0] / boo[1] / boo[2] point to the same objects as foo[0] / foo[1] / foo[2] ? 回答1: boo[0] does point to the same object as foo[0] . But doing boo[0] = 5 does not modify the object referred to by boo[0] ; it modifies the object referred to by boo . Assigning to an element of a list modifies the

Python list slice as shallow copy

孤街浪徒 提交于 2021-02-11 06:19:58
问题 foo = [1, 2, 3] foo[:][0] = 5 foo doesn't change, also: import copy foo = [1, 2, 3] boo = copy.copy(foo) boo[0] = 5 Again, foo[0] stays the same. Why? The shallow copy creates new list, but shouldn't boo[0] / boo[1] / boo[2] point to the same objects as foo[0] / foo[1] / foo[2] ? 回答1: boo[0] does point to the same object as foo[0] . But doing boo[0] = 5 does not modify the object referred to by boo[0] ; it modifies the object referred to by boo . Assigning to an element of a list modifies the

Python list slice as shallow copy

放肆的年华 提交于 2021-02-11 06:18:09
问题 foo = [1, 2, 3] foo[:][0] = 5 foo doesn't change, also: import copy foo = [1, 2, 3] boo = copy.copy(foo) boo[0] = 5 Again, foo[0] stays the same. Why? The shallow copy creates new list, but shouldn't boo[0] / boo[1] / boo[2] point to the same objects as foo[0] / foo[1] / foo[2] ? 回答1: boo[0] does point to the same object as foo[0] . But doing boo[0] = 5 does not modify the object referred to by boo[0] ; it modifies the object referred to by boo . Assigning to an element of a list modifies the

Shallow copy for arrays, why can't simply do newArr = oldArr?

ぃ、小莉子 提交于 2021-02-06 09:19:51
问题 Let's say I have an array of integers, "orig" I want to shallow copy it, so can't I just do this: int[] shallow = orig; My professor said that for primitives, shallow and deep copy are essentially the same, in that we have to copy over each index of the array. But setting the whole array equals to another array does the same thing, right? I have a similar question with object arrays This was my ideology Book[] objArr2 = objArr1; But I was told that I would have to copy each array index over,

Shallow copy of a hashset

社会主义新天地 提交于 2021-02-04 11:54:02
问题 Whats the best way of doing it? var set2 = new HashSet<reference_type>(); Traverse the set with a foreach like this. foreach (var n in set) set2.Add(n); Or use something like union like this. set2 = set.UnionWith(set); // all the elements 回答1: Use the constructor: HashSet<type> set2 = new HashSet<type>(set1); Personally I wish LINQ to Objects had a ToHashSet extension method as it does for List and Dictionary . It's easy to create your own of course: public static HashSet<T> ToHashSet<T>(this

Shallow copy of a hashset

喜夏-厌秋 提交于 2021-02-04 11:53:26
问题 Whats the best way of doing it? var set2 = new HashSet<reference_type>(); Traverse the set with a foreach like this. foreach (var n in set) set2.Add(n); Or use something like union like this. set2 = set.UnionWith(set); // all the elements 回答1: Use the constructor: HashSet<type> set2 = new HashSet<type>(set1); Personally I wish LINQ to Objects had a ToHashSet extension method as it does for List and Dictionary . It's easy to create your own of course: public static HashSet<T> ToHashSet<T>(this

I have two lists containing the same objects. How do I change one list without changing the other? [duplicate]

倖福魔咒の 提交于 2020-12-25 18:25:22
问题 This question already has answers here : How do you make a deep copy of an object? (19 answers) Closed 4 years ago . I first noticed this problem when I only put the objects in listOfRates and then created inverseListOfRates by copying it. But even using this method, I can't alter one list without altering the other. How do I solve this issue? List<HistoricRate> listOfRates = new ArrayList<HistoricRate>(); List<HistoricRate> inverseListOfRates = new ArrayList<HistoricRate>(); for

I have two lists containing the same objects. How do I change one list without changing the other? [duplicate]

自作多情 提交于 2020-12-25 18:17:20
问题 This question already has answers here : How do you make a deep copy of an object? (19 answers) Closed 4 years ago . I first noticed this problem when I only put the objects in listOfRates and then created inverseListOfRates by copying it. But even using this method, I can't alter one list without altering the other. How do I solve this issue? List<HistoricRate> listOfRates = new ArrayList<HistoricRate>(); List<HistoricRate> inverseListOfRates = new ArrayList<HistoricRate>(); for

I have two lists containing the same objects. How do I change one list without changing the other? [duplicate]

♀尐吖头ヾ 提交于 2020-12-25 18:11:28
问题 This question already has answers here : How do you make a deep copy of an object? (19 answers) Closed 4 years ago . I first noticed this problem when I only put the objects in listOfRates and then created inverseListOfRates by copying it. But even using this method, I can't alter one list without altering the other. How do I solve this issue? List<HistoricRate> listOfRates = new ArrayList<HistoricRate>(); List<HistoricRate> inverseListOfRates = new ArrayList<HistoricRate>(); for