隐含的复制构造函数并不总是适用的,因为它完成的只是浅复制。
对象的浅复制
1 #include<iostream> 2 #include<cassert> 3 using namespace std; 4 class Point{ 5 public: 6 Point() :x(0), y(0)//默认构造函数 7 { 8 cout << "constructor1" << endl; 9 } 10 Point(int x, int y) :x(x), y(y) 11 { 12 cout << "constructor2" << endl; 13 } 14 ~Point() 15 { 16 cout << "destructor" << endl; 17 } 18 int getX() 19 { 20 return x; 21 } 22 int getY() 23 { 24 return y; 25 } 26 void move(int mx, int my) 27 { 28 x += mx; 29 y += my; 30 } 31 private: 32 int x, y; 33 }; 34 35 class PointArray{ 36 public: 37 PointArray(int size) :size(size) 38 { 39 ptr = new Point[size]; 40 } 41 42 ~PointArray() 43 { 44 cout << "析构" << endl; 45 delete[] ptr; 46 } 47 48 Point &getElement(int index) 49 { 50 assert(index >= 0 && index < size);//如果数组下标越界,程序终止 51 return ptr[index]; 52 } 53 54 private: 55 Point *ptr; 56 int size; 57 }; 58 59 int main() 60 { 61 int size; 62 cout << "请输入size的值:"; 63 cin >> size; 64 65 PointArray pointArray1(size); 66 pointArray1.getElement(0).move(1, 1); 67 pointArray1.getElement(1).move(3, 3); 68 69 PointArray pointArray2 = pointArray1; 70 cout << pointArray2.getElement(0).getX() << "," << pointArray2.getElement(0).getY() << endl; 71 cout << pointArray2.getElement(1).getX() << "," << pointArray2.getElement(1).getY() << endl; 72 73 pointArray1.getElement(0).move(7, 7); 74 pointArray1.getElement(1).move(11, 11); 75 cout << pointArray2.getElement(0).getX() << "," << pointArray2.getElement(0).getY() << endl; 76 cout << pointArray2.getElement(1).getX() << "," << pointArray2.getElement(1).getY() << endl; 77 78 return 0; 79 }
pointArray1和pointArray2的成员ptr指向的是同一内存地址,表面上好像完成了复制,但是并没有形成真正的副本。因此当程序移动pointArray1中的点时,也影响到了pointArray2。这种效果就是浅复制。
浅复制还有更大的弊病:程序结束前,pointArray1和pointArray2的析构函数会自动被调用,动态内存空间会被释放。由于两个对象共用同一块内存空间,因此该空间被两次释放,于是导致运行错误。
对象的深复制
1 #include<iostream> 2 #include<cassert> 3 using namespace std; 4 5 class Point{ 6 public: 7 Point() :x(0), y(0)//默认构造函数 8 { 9 cout << "constructor1" << endl; 10 } 11 Point(int x, int y) :x(x), y(y) 12 { 13 cout << "constructor2" << endl; 14 } 15 ~Point() 16 { 17 cout << "destructor" << endl; 18 } 19 int getX() 20 { 21 return x; 22 } 23 int getY() 24 { 25 return y; 26 } 27 void move(int mx, int my) 28 { 29 x += mx; 30 y += my; 31 } 32 private: 33 int x, y; 34 }; 35 36 class PointArray{ 37 public: 38 PointArray(int size) :size(size) 39 { 40 ptr = new Point[size]; 41 } 42 43 PointArray(const PointArray &pArray) 44 { 45 size = pArray.size; 46 ptr = new Point[size]; 47 for (int i = 0; i < size; i++) 48 { 49 ptr[i] = pArray.ptr[i]; 50 } 51 } 52 53 ~PointArray() 54 { 55 cout << "析构" << endl; 56 delete[] ptr; 57 } 58 59 Point &getElement(int index) 60 { 61 assert(index >= 0 && index < size);//如果数组下标越界,程序终止 62 return ptr[index]; 63 } 64 65 private: 66 Point *ptr; 67 int size; 68 }; 69 70 int main() 71 { 72 int size; 73 cout << "请输入size的值:"; 74 cin >> size; 75 76 PointArray pointArray1(size); 77 pointArray1.getElement(0).move(1, 1); 78 pointArray1.getElement(1).move(3, 3); 79 80 PointArray pointArray2 = pointArray1; 81 cout << pointArray2.getElement(0).getX() << "," << pointArray2.getElement(0).getY() << endl; 82 cout << pointArray2.getElement(1).getX() << "," << pointArray2.getElement(1).getY() << endl; 83 84 pointArray1.getElement(0).move(7, 7); 85 pointArray1.getElement(1).move(11, 11); 86 cout << pointArray2.getElement(0).getX() << "," << pointArray2.getElement(0).getY() << endl; 87 cout << pointArray2.getElement(1).getX() << "," << pointArray2.getElement(1).getY() << endl; 88 89 return 0; 90 }
移动pointArray1中的点不再影响pointArray2中的点,而且程序结束前分别释放内存空间。
时间: 2024-10-28 22:45:01