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...
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...
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?
...
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...
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...
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?
...
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...
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...
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...
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.
...
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 == ...
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...
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?
...
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.
...
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...
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 ...
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
{
// ...
}
...
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...
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...
#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 ...