| using namespace System; using namespace System::Collections; void PrintEntries(String^ s, ArrayList^ aList); int main() { ArrayList^ al1 = gcnew ArrayList; /*1*/ al1->Add("Red"); al1->Add("Blue"); al1->Add("Green"); al1->Add("Yellow"); /*2*/ PrintEntries("al1", al1); /*3*/ ArrayList^ al2 = static_cast<ArrayList^>(al1->Clone()); /*4*/ PrintEntries("al2", al2); /*5*/ al1->Remove("Blue"); al1->Add("Black"); al1->RemoveAt(0); al1->Insert(0, "Brown"); /*6*/ PrintEntries("al1", al1); /*7*/ PrintEntries("al2", al2); } void PrintEntries(String^ s, ArrayList^ aList) { Console::Write("{0}: ", s); for each(Object^ o in aList) { Console::Write("\t{0}", o); } Console::WriteLine(); } |
| al1: Red Blue Green Yellow al2: Red Blue Green Yellow al1: Brown Green Yellow Black al2: Red Blue Green Yellow |
ArrayList al1由4个代表不同颜色的字符串组成,通过在标记3中调用ArrayList::Clone函数,可以对此对象作一个完整的复制,所以,标记2与4表示的输出完全相同。
接下来,从al1中移除了第二个元素,在末尾加入了一个新的元素,并修改了第一个元素的值。当把标记6与7表示的输出进行一个对比时,你会发现,对al1所作的修改,完全不会影响到al2。在此需要说明的是,al2内部的引用,指向其自身元素的私有副本,而不是al1中的元素,这就是通常提到的"深拷贝",反之,只是简单地把两个ArrayList内部引用指向同一个值集(如al2=al1的赋值操作),这称为"浅拷贝"。
也就是说,如果你希望复制所拥有的对象,应该参照库函数Clone机制中的复制过程。
| public ref class Point : ICloneable { // ... public: virtual Object^ Clone() { return MemberwiseClone(); } }; int main() { /*1*/ Point^ p1 = gcnew Point(3, 5); /*2*/ Console::WriteLine("p1: {0}", p1); /*3*/ Point^ p2 = static_cast<Point^>(p1->Clone()); /*4*/ p1->Move(9, 11); /*5*/ Console::WriteLine("p1: {0}", p1); /*6*/ Console::WriteLine("p2: {0}", p2); } |
| p1: (3,5) p1: (9,11) p2: (3,5) |
| protected: Object^ MemberwiseClone(); |
| x->MemberwiseClone() != x x->MemberwiseClone()->GetType() == x->GetType() |
通常来说,复制一个对象必须创建对象的一个新实例,但同时也可能需要对内部数据结构进行复制,在此不需要调用任何的构造函数。
Object::MemberwiseClone执行一个详细而精确的克隆操作。它创建类对象的一个新实例,并用源对象字段内容,初始化对应的所有字段,就好像在赋值;但是要注意的是,字段内容本身并没有被克隆,所以,这个函数执行的是一个对象的"浅拷贝"。
| using namespace System; public ref class Circle : ICloneable { Point^ origin; float radius; public: property Point^ Origin { Point^ get() { return static_cast<Point^>(origin->Clone()); } } void SetOrigin(int x, int y) { origin->X = x; origin->Y = y; } void SetOrigin(Point^ p) { SetOrigin(p->X, p->Y); } property double Radius { double get() { return radius; } void set(double value) { radius = static_cast<float>(value); } } Circle() { origin = gcnew Point; SetOrigin(0, 0); Radius = 0.0; } Circle(int x, int y, double r) { origin = gcnew Point; SetOrigin(x, y); Radius = r; } Circle(Point^ p, double r) { origin = gcnew Point; SetOrigin(p->X, p->Y); Radius = r; } virtual String^ ToString() override { return String::Concat("{", Origin, ",", Radius, "}"); } virtual Object^ Clone() { /*1*/ Circle^ c = static_cast<Circle^>(MemberwiseClone()); /*2*/ c->origin = static_cast<Point^>(origin->Clone()); /*3*/ return c; } }; |
【中国下载站】【设为主页】【收藏本页】【打印本文】【回到顶部】【关闭此页】