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
2
votes
3 answers

Get values from HashMap as reference

Is it possible to get list of values from HashMap as a reference class MyCustomObject { String name; Integer id; MyCustomObject(String name, Integer id){ this.name = name; this.id = id; } } HashMap
user4774371
2
votes
1 answer

Why do the assignments to v, w behave differently?

I would expect a shallow copy on v and output 701 801, but I see 700 801. I cannot see a good explanation. If w is a shallow copy, why not v? Does an Integer assigned an Integer "rebox"? class Scalar { Integer w; public String toString() {…
2
votes
3 answers

Why isnt this object null after a shallow copy?

Below is my copy of spouse to client (both are same object type). Spouse is then set to null. client = spouse; // Copying data spouse = null; I then pause (using a breakpoint on a different line) and check the memory of client and spouse. spouse…
Robert
  • 93
  • 1
  • 7
2
votes
1 answer

Python list slice as shallow copy

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…
user5670001
2
votes
1 answer

Copy all members of a class to current object

Let's assume I have a base class called BaseClass and an object called Foo that inherited from BaseClass. Can I copy all members of a BaseClass instance to current object being created instead of do it manually? class BaseClass { int a; int…
Jack
  • 16,276
  • 55
  • 159
  • 284
2
votes
3 answers

Shallow Copy From Inherited Classes

Ok so I have an abstract base class called Product, a KitItem class that inherits Product and a PackageKitItem class that inherits KitItem. ie. Product KitItem : Product PackageKitItem : KitItem I have my KitItems loaded and I need to load up a…
Echostorm
  • 9,678
  • 8
  • 36
  • 50
2
votes
3 answers

In Java, are fields of array type deep copied or shallow copied?

[Background note: I am a new Java programmer with a C++ background, so is a little confused about how arguments are passed around in Java.] While reading and writing some code, I came to the following case public class B{ int[] intArr; …
CloudyTrees
  • 711
  • 2
  • 10
  • 20
2
votes
1 answer

Lo-Dash _.pick()

Why documentation on page: Lo-Dash documentation Says: _.pick(object, [callback], [thisArg]) Creates a shallow clone of object composed of the specified properties. Property names may be specified as individual arguments or as arrays of property…
Sysrq147
  • 1,359
  • 4
  • 27
  • 49
2
votes
4 answers

C++ Shallow and deep copying - reflecting changes in the num_items of a vector

I'm currently undertaking a C++ course at university. I understand the general concept of shallow and deep copying using vectors however there's an example in my textbook that has me confused. Please assume that it is a poorly implemented vector…
jules
  • 63
  • 3
  • 10
2
votes
4 answers

Why is the Object class's clone() method giving a deep copy of object?

As per the JAVA documentation, the super.clone() when called returns a shallow copy of the object. In the code below I have two objects name and id; and one primitive variable num. When the super.clone() method is called on the first object, it…
Annu
  • 145
  • 1
  • 3
  • 10
2
votes
1 answer

Copying element of existing array to a a new array without using splice?

I'm currently doing some coursework for university. I am trying to copy an individual value of an old array to a new array, then setting the old arrays value to 0. Obviously if i just assign the value to the new array, then alter the old arrays…
mwild
  • 1,483
  • 7
  • 21
  • 41
2
votes
4 answers

Creating a shallow copy of structures in C#

I tried to search for my answer and found them in regards to C and not C# so thought of posting it. My question might be trivial here. As per my understanding (in simple terms) After Copying has completed Shallow Copy -> main and copied object…
samar
  • 5,021
  • 9
  • 47
  • 71
2
votes
3 answers

git shallow clone along with branch

I have to deal with a git repo that contains some binaries. I would be REALLY grateful if someone could explain this to me >git clone --depth 1 -- ssh://git/foo/bar.git test_d Cloning into 'test_d'... remote: Counting objects: 289, done. remote:…
GiM
  • 460
  • 4
  • 13
1
vote
1 answer

What are the differences between applying list methods and reassignment on a list slice?

I tried these codes on a list slice a[:] when I was learning list methods: a = list('zyx') a[:].pop() print(a) a = list('zyx') a[:].append('o') print(a) a = list('zyx') a[:].clear() print(a) a = list('zyx') a[:] = [] print(a) and the results I…
1
vote
0 answers

How to copy a list with reference so the changes to the copied version also apply to the original one in C++?

In my homework I have to make the FooCl class for these: int main() { int x[] = {2, 3, 7, 9, 21, 11, 54, 91}; FooCl itemsI(x, sizeof(x) / sizeof(x[0])); std::string s[] = {"Car", "Bus", "Taxi", "Bike"}; FooCl
Dodande
  • 97
  • 5