深层复制与浅层复制最主要的区别在于:
1.浅层复制只是对指针的复制,两个对象中的指针实际上指向同一块内存区域,所以改变其中任意一个的值,都会对另一个产生影响;
2.深层复制是对元素值的复制,复制构造函数会重新申请一块大小合适的内存空间来存储原来元素的值,所以深层复制后的对象与原对象之间并无关联关系。
/*---------------------------------------
功能:对象的深层复制
运行结果:
Input the number of point: 2
Calling default constructor of Point...
Calling default constructor of Point...
Calling constructor of ArrayPoint...
Calling default constructor of Point...
Calling default constructor of Point...
The original position of points2:
(1, 2)
(3, 4)
The changed position of points1:
(5, 6)
(7, 8)
The current position of points1:
(1, 2)
(3, 4)
Calling destructor of PointArray...
Calling destructor of Point...
Calling destructor of Point...
Calling destructor of PointArray...
Calling destructor of Point...
Calling destructor of Point...
-----------------------------------------
Author: Zhang Kaizhou
Date: 2019-4-4 18:04:03
---------------------------