Questions tagged [shallow-copy]

A shallow copy of an object is a duplicate that may not be fully independent of the original, in the sense that any references / pointers it holds to other objects refer to the same objects that the original's do. Use this tag for questions regarding implementing or using shallow copying methods.

Shallow copying is a process by which an object is duplicated to create a copy that may not be wholly independent of the original. Also known as "field by field" copying, this differs from deep copying with respect to the treatment of references / pointers to other objects: shallow copying copies the references / pointers held by the original, whereas deep copying makes a deep copy of each referenced object, too. Shallow copying is "shallow" in the sense that it copies only the original object itself, not any appearing deeper in its reference graph.

Related tags

Links

394 questions
7
votes
3 answers

Can I write different copyCtor for const and non-const instances?

I have the following problem: I have a class which should do this: Obj o; Obj o1(o), o1=o; // deep-copies const Obj c(o), c=o; // deep-copies const Obj c1(c), c1=c; // shallow-copies Obj o2(c), o2=c; // deep-copies How can I do this preferably…
Barney Szabolcs
  • 11,846
  • 12
  • 66
  • 91
6
votes
2 answers

What is actually moving a variable in Rust?

I have never experimented with languages like C, C++, Go, etc., and I decided to start with Rust, I already understand a little what the stack and the Heap are, but, what does it really mean by moving a variable, the documentation says that it is…
Grizzly
  • 371
  • 2
  • 13
6
votes
3 answers

VB.NET, Is Object Returned by Reference from Function

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…
Ian
  • 4,169
  • 3
  • 37
  • 62
6
votes
1 answer

why should I make a *shallow* copy of a dataframe?

related to why should I make a copy of a data frame in pandas I noticed that in the popular backtesting library, def __init__(self, data: pd.DataFrame) data = data.copy(False) in row 631. What's the purpose of such a copy?
ihadanny
  • 4,377
  • 7
  • 45
  • 76
6
votes
3 answers

Default copy constructor in cpp shallow or deep copy?

Does the default copy-constructor do a shallow or a deep copy in C++? I am really confused with default copy constructor in cpp, as it does shallow copy or deep copy, as when I did v2=v1; suppose v1={1,2,3}, now if I have done v2[0]=1; It does not…
6
votes
3 answers

Does LINQ new up memory when creating returns

Does LINQ actually perform a deep copy of the results to a different list/array/etc, or does it simply give me a list/array/etc. composed of references to the original?
Joel B
  • 12,082
  • 10
  • 61
  • 69
6
votes
1 answer

Returning a new structure with fields changed

I'm looking for a simple way to return a new structure which is a copy of an existing one with some fields changed, without modifying the original. I get that you can use setf to change the data in one of the fields, like so: [1]> (defstruct foo…
Electric Coffee
  • 11,733
  • 9
  • 70
  • 131
6
votes
3 answers

Selective shallow copy from one array to another

Assuming I have 2 array of different size i.e int arr[] = {0,1,2,3,4,5,6,7,8,9}; int *arr2 = new int[5]; I want to shallow copy some of them, Deep copy equivalent would be int j =0; if(!(i%2)) { arr2[j]=arr[i]; j++; …
new2code
  • 61
  • 4
6
votes
4 answers

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

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…
onepiece
  • 3,279
  • 8
  • 44
  • 63
5
votes
2 answers

Avoiding ConcurrentModificationException on List by making a shallow copy

I have a class like the following: class Test { private LinkedList persons = new LinkedList; public synchronized void remove(Person person) { persons.remove(person); } public List
His
  • 5,891
  • 15
  • 61
  • 82
5
votes
4 answers

How would you improve this shallow copying class?

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,…
Neil Barnwell
  • 41,080
  • 29
  • 148
  • 220
5
votes
1 answer

Make a shallow copy in data.table

I read in an SO topic an answer from Matt Dowle about a shallow function to make shallow copies in data.table. However, I can't find the topic again. data.table does not have any exported function called shallow. There is an internal one but not…
JRR
  • 3,024
  • 2
  • 13
  • 37
5
votes
4 answers

AngularJS : copy vs extend

Explanation : we come across some situation in which we need to copy one object to another object. In that case, we probably have two solutions: angular.copy() or angular.extend(). Challenge i am facing : As we know angular.copy(source, destination)…
Debug Diva
  • 26,058
  • 13
  • 70
  • 123
5
votes
1 answer

Why setting a dict shallow copy to itself?

I think it's a bit weird question to ask. The thing is that while I was studying some parts of django code I came across something I've never seen before. According to Copy Difference Question and It's usage in dictionary we can create two…
Heartagramir
  • 344
  • 1
  • 2
  • 9
5
votes
2 answers

Intercept C++ implicit copy constructor, or invoke its functionality

Given: class Foo { private: static int cntFoos; //... stuff... public: Foo() { cntFoos++; } ~Foo() { cntFoos--; } }; ... where "stuff" may be any set of properties. (The idea is to have a counter of instances of that…
tru7
  • 6,348
  • 5
  • 35
  • 59