Hi Guys
NP95 shallow copy/deep copy
If
p3: X: 100 Y: 100
p4: X: 100 Y: 100
I mean this is shallow copy
If
p3: X: 100 Y: 100
p4: X: 0 Y: 100
I mean this is deep copy
That means if p3 is independent of p4 it is a deep copy
In this program MemberwiseClone() is producing deep copy.
I wish to know whether my understanding is correct.
Please explain.
Thank you
using System;
// The Point class supports deep copy semantics a la ICloneable.
public class Point : ICloneable
{
public int x, y;
public Point(int x, int y)
{
this.x = x;
this.y = y;
}
// Return a copy of the current object.
public object Clone()
{
// Copy each field of the Point member by member.
return this.MemberwiseClone();
}
public override string ToString()
{
return "X: " + x + " Y: " + y;
}
}
public class CloneDemo
{
public static void
{
// Notice Clone() returns a generic object type.
// You must perform an explicit cast to obtain the derived type.
Point p3 = new Point(100, 100);
Point p4 = (Point)p3.Clone();
// Change p4.x (which will not change p3.x).
p4.x = 0;
// Print each object.
Console.WriteLine("p3: {0}", p3);
Console.WriteLine("p4: {0}", p4);
}
}
/*
p3: X: 100 Y: 100
p4: X: 0 Y: 100
*/
Posted Apr 19, 2008, 4:42 PM
Thank you, Alan.
AlanPosted Apr 19, 2008, 8:43 AM
Well, as Manish said, a compound object is an object which contains other objects.
If we take the narrow definition of object (i.e. a reference type instance created on the heap), then a Point object (as modified) is a compound object, because it contains a Color object. The latter is also a compound object because it contains a string object.
Posted Apr 19, 2008, 5:52 AM
What is meant by compound objects? In my understanding compound objects in the above program is p4.c.Name = "Blue";. Because this code contains more than one object. I wish to know whether my understanding is correct. Please explain.
AlanPosted Apr 18, 2008, 6:37 PM
This modifed version now includes a reference type field of type Color:
using System;
public class Color
{
public string Name;
public Color (string name)
{
Name = name;
}
}
// The Point class supports deep copy semantics a la ICloneable.
public class Point : ICloneable
{
public int x, y;
public Color c;
public Point(int x, int y, Color c)
{
this.x = x;
this.y = y;
this.c = c;
}
// Return a copy of the current object.
public object Clone()
{
// Copy each field of the Point member by member.
return this.MemberwiseClone();
}
public override string ToString()
{
return "X: " + x + " Y: " + y + "C: " + c.Name;
}
}
public class CloneDemo
{
public static void Main()
{
// Notice Clone() returns a generic object type.
// You must perform an explicit cast to obtain the derived type.
Point p3 = new Point(100, 100, new Color("Red"));
Point p4 = (Point)p3.Clone();
// change p4.c.Name which will change p3.c.Name as it's the same Color object
p4.c.Name = "Blue";
// Print each object.
Console.WriteLine("p3: {0}", p3);
Console.WriteLine("p4: {0}", p4);
// Change p4.c which will not change p3.c as it's a different Color object
p4.c = new Color("Green");
// Print each object again.
Console.WriteLine();
Console.WriteLine("p3: {0}", p3);
Console.WriteLine("p4: {0}", p4);
}
}
Posted Apr 18, 2008, 2:19 PM
It is much appreciated if you modify the above program that supports compound objects.
Blocked AccountPosted Apr 18, 2008, 8:13 AM
Hi Friend.
The difference between shallow and deep copying is only relevant for compound objects (objects that contain other objects, like lists or class instances):
A shallow copy constructs a new compound object and then (to the extent possible) inserts references into it to the objects found in the original.
A deep copy constructs a new compound object and then, recursively, inserts copies into it of the objects found in the original.
Happy Coding!!