"raylopez99" <ra********@yahoo.comwrote in message
news:11**********************@22g2000hsm.googlegro ups.com...
In C++, you have symbolic overriding of "+", "=", etc, which C# also
has. This question is not really about that.
Rather, in C#, when you say:
MyObject X = new MyObject();
MyObject Y = new MyObject();
X = Y; //what does this '=' mean?
X is not an instance of MyObject; it is a reference to an instance of
MyObject. Basically X points to an instance.
X = Y // X now points to a different instance.
In C# you can reasign references and even declare null references (you
can get a null reference in C++, but that generally a design flaw)
The sample above would look like this in C++
MyObject *X = new MyObject();
MyObject *Y = new MyObject();
X = Y; //what does this '=' mean?
Simply reasigning a pointer (although it is also a memory leak in C++)
Is "X=Y" a shallow copy? I think it is.
Nope, simply a copy of a reference
In C++ you can do the following (Value semantics)
MyObject X; // X is an instance (on the stack)
MyObject Y; // Y is an instance (on the stack)
X=Y //Shallow copy from the second instance into the first instance
or you can do (reference semantics)
MyObject *X = new MyObject(); // X is a pointer to a instance of
MyObject (on the heap)
MyObject *Y = new MyObject(); // Y is a pointer to a instance of
MyObject (on the heap)
X = Y; // copy a pointer (memory leak)
So
MyObject X = new MyObject();
MyObject Y = new MyObject();
X = Y;
The only thing that is copied is the reference. Both X and Y point to
the same instance
Hope this helps
Bill