shallow-copy

How do I create a copy of an object in PHP?

It appears that in PHP objects are passed by reference. Even assignment operators do not appear to be creating a copy of the Object. Here's a simple, contrived proof: <?php class A { public $b; } function set_b($obj) { $obj->b = "after"; } $a = new A(); $a->b = "before"; $c = $a; //i would especially expect this to create a cop...

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 collection of PackageKitItems which are, effectively, shallow copies of KitItems. Currentl...

Python list slice syntax used for no obvious reason

I occasionally see the list slice syntax used in Python code like this: newList = oldList[:] Surely this is just the same as: newList = oldList Or am I missing something? ...

What are the implications of performing a shallow copy on an array in order to resize it?

If my understanding of deep and shallow copying is correct my question is an impossible one. If you have an array (a[10]) and perform a shallow copy (b[20]) wouldn't this be impossible as the data in b wouldn't be contiguous? If i've got this completely wrong could someone advise a fast way to immitate (in c#) c++'s ability to do a real...

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, but what improvements would you make? Here's the code: public class ShallowCopy { publ...

How do strings work when shallow copying something in C#?

Strings are considered reference types yet can act like values. When shallow copying something either manually or with the MemberwiseClone(), how are strings handled? Are they considred separate and isolated from the copy and master? ...

Shallow Copy - Reference type anomalous nature

I cannot understand the output of the two sets of code snippets given below. How don't really get the concept of shallow copy. How can it be explained? Class: public class Person : ICloneable { public string Name; public int[] arr; public object Clone() { return this.MemberwiseClone(); } } Code...

Question about array shallow copy in C#

Just to make sure I'm understanding shallow copies of reference types correctly and that I'm not constructing a huge memory leak here: // Adds text to the beginning of the log RTB // Also keeps the log RTB trimmed to 100 lines var lines = new string[rtbLog.Lines.Length + 1]; lines[0] = "text"; Array.Copy(rtbLog.Lines, 0, lines, 1, rtbLo...

In Java, what is a shallow copy?

java.util.Calendar.clone() returns "...a new Calendar with the same properties" and returns "a shallow copy of this Calendar". This does not appear to be a shallow copy as answered here on SO. That question is tagged language-agnostic, Java does not seem to follow the language agnostic definition. As I step through the code I notice t...

How to copy a list in Scala

I want to shallow copy a list in Scala. I wanted to do somehing like: val myList = List("foo", "bar") val myListCopy = myList.clone But the clone method is protected. ...

Do shallow copies share pointers? (C++)

I know that if I do something like this: class Obj { public: int* nine; }; Obj Obj1; //Awesome name int eight = 8; Obj1.nine = &eight; Obj Obj2 = Obj1; //Another Awesome name then Obj1's and Obj2's nines will point to the same 8, but will they share the same pointer? I.e.: int Necronine = 9; Obj1.nine = &Necronine; Obj2.nine == ...

C++: shallow/deep copy of std::map

How would I best implement these? I thought of something like this: using namespace std; shape_container shape_container::clone_deep () const { shape_container* ptr = new shape_container(); copy( data.begin(), data.end(), (*ptr).begin() ); return *ptr; } shape_container shape_contai...

How do I make a shallow copy of a Perl hash reference?

I want to push a reference to a hash. By that I mean I want to push a reference to a new hash that is a shallow copy of the hash I am given. How do I create the shallow copy? ...

Is shallow copy sufficient for structures with char[]?

I have a structure containing character arrays with no any other member functions. I am doing assignment operation between two instances of these structures. If I'm not mistaken, it is doing shallow copy. Is shallow copy safe in this case? I've tried this in C++ and it worked but I would just like to confirm if this behavior is safe. ...

Shallow copy of a Map in Java

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, Ob...

Can I use memcpy in C++ to copy classes that have no pointers or virtual functions

Say I have a class, something like the following; class MyClass { public: MyClass(); int a,b,c; double x,y,z; }; #define PageSize 1000000 MyClass Array1[PageSize],Array2[PageSize]; If my class has not pointers or virtual methods, is it safe to use the following? memcpy(Array1,Array2,PageSize*sizeof(MyClass)); The reason I ...

Copying objects to 'this' object in C#

I have a certain hirerchy of classes that needs the capeability to copy all public properties from one object to another. Each class has a certain set of public properties that might differ from any other class. Example: class Base { // Common properties/methods... public void Copy<T>(T data) where T : Base { // ... } ...

Library of Objects - Access Index Value or Object Itself? (e.g., deep vs. shallow copy perhaps?)

I've always been confused/unsure about how .Net copies references. Let's say I have a Bitmap object for GDI+. dim foo as new bitmap("c:\foo.bmp") 'Foo' holds the bitmap object. Now let's say I do this. dim bar as bitmap = foo Is this a shallow copy or a deep copy? If I set foo equal to nothing, does bar suddenly reference 'nothing...

How to shallow copy app engine model instance to create new instance?

I want to implement a simple VersionedModel base model class for my app engine app. I'm looking for a pattern that does not involve explicitly choosing fields to copy. I am trying out something like this, but it is to hacky for my taste and did not test it in the production environment yet. class VersionedModel(BaseModel): is_histo...

Creating clone of an object not working with virtual base class

#include<iostream> using namespace std; class Something { public: int j; Something():j(20) {cout<<"Something initialized. j="<<j<<endl;} }; class Base { private: Base(const Base&) {} public: Base() {} virtual Base *clone() { return new Base(*this); } virtual void ID() { cout<<"BASE"<<endl; } }; class Derived ...