c++中深层复制(浅层复制运行错误)成功运行-----sample
2024-08-30 00:10:20
下面随笔给出c++中深层复制(浅层复制运行错误)成功运行------sample。
浅层复制与深层复制
浅层复制
实现对象间数据元素的一一对应复制。
深层复制
当被复制的对象数据成员是指针类型时,不是复制该指针成员本身,而是将指针所指对象进行复制。
浅层复制-系统默认复制构造函数(运行错误)
1 //例 对象的浅层复制
2
3 #include <iostream>
4
5 #include <cassert>
6
7 using namespace std;
8
9 class Point {
10
11 //类的声明略
12
13 //……
14
15 };
16
17 class ArrayOfPoints {
18
19 //类的声明略
20
21 //……
22
23 };
24
25
26
27 int main() {
28
29 int count;
30
31 cout << "Please enter the count of points: ";
32
33 cin >> count;
34
35 ArrayOfPoints pointsArray1(count); //创建对象数组
36
37 pointsArray1.element(0).move(5,10);
38
39 pointsArray1.element(1).move(15,20);
40
41
42
43 ArrayOfPoints pointsArray2(pointsArray1); //创建副本
44
45
46
47 cout << "Copy of pointsArray1:" << endl;
48
49 cout << "Point_0 of array2: " << pointsArray2.element(0).getX() << ", "
50
51 << pointsArray2.element(0).getY() << endl;
52
53 cout << "Point_1 of array2: " << pointsArray2.element(1).getX() << ", "
54
55 << pointsArray2.element(1).getY() << endl;
56
57 pointsArray1.element(0).move(25, 30);
58
59 pointsArray1.element(1).move(35, 40);
60
61
62
63 cout<<"After the moving of pointsArray1:"<<endl;
64
65
66
67 cout << "Point_0 of array2: " << pointsArray2.element(0).getX() << ", "
68
69 << pointsArray2.element(0).getY() << endl;
70
71 cout << "Point_1 of array2: " << pointsArray2.element(1).getX() << ", "
72
73 << pointsArray2.element(1).getY() << endl;
74
75
76
77 return 0;
78
79 }
运行结果如下:
1 Please enter the number of points:2
2
3 Default Constructor called.
4
5 Default Constructor called.
6
7 Copy of pointsArray1:
8
9 Point_0 of array2: 5, 10
10
11 Point_1 of array2: 15, 20
12
13 After the moving of pointsArray1:
14
15 Point_0 of array2: 25, 30
16
17 Point_1 of array2: 35, 40
18
19 Deleting...
20
21 Destructor called.
22
23 Destructor called.
24
25 Deleting... //接下来程序出现运行错误。
深层复制-自己定义的复制构造函数(运行正确)
1 //例 对象的深层复制
2
3 #include <iostream>
4
5 #include <cassert>
6
7 using namespace std;
8
9 class Point { //类的声明略
10
11 };
12
13 class ArrayOfPoints {
14
15 public:
16
17 ArrayOfPoints(const ArrayOfPoints& pointsArray);
18
19 //其他成员略
20
21 };
22
23 ArrayOfPoints::ArrayOfPoints(const ArrayOfPoints& v) {
24
25 size = v.size;
26
27 points = new Point[size];
28
29 for (int i = 0; i < size; i++)
30
31 points[i] = v.points[i];
32
33 }
34
35 int main() {
36
37 //同
38
39 }
程序的运行结果如下:
1 Please enter the number of points:2
2
3 Default Constructor called.
4
5 Default Constructor called.
6
7 Default Constructor called.
8
9 Default Constructor called.
10
11 Copy of pointsArray1:
12
13 Point_0 of array2: 5, 10
14
15 Point_1 of array2: 15, 20
16
17 After the moving of pointsArray1:
18
19 Point_0 of array2: 5, 10
20
21 Point_1 of array2: 15, 20
22
23 Deleting...
24
25 Destructor called.
26
27 Destructor called.
28
29 Deleting...
30
31 Destructor called.
32
33 Destructor called.
最新文章
- ie8 jquery parents() 获取多个的问题
- redis-介绍与比较
- PHP 出现 502 解决方案
- Oracle 游标使用全解(转)
- webservice调用服务端数据时给soapenv:Envelope 添加自定义的命名空间
- OnScrollListener分页加载
- 阿里云产品介绍(一):云服务器ECS
- 江湖恩仇录之PHP程序CPU高占用优化经历分享
- [转]如何编译tizen源码(图文教程)?
- hdu 4143 A Simple Problem (变形)
- Visual Studio 2010 将网站直接发布到远程站点
- java学习初体验之课后习题
- hdu 1407 测试你是否和LTC水平一样高
- Spring框架学习之依赖注入
- django事务处理
- java数组及数组的插入,删除,冒泡算法
- HSRP(Hot Standby Router Protocol)
- flask框架----flask基础
- python中bottle模块的使用
- 滑动窗口解决Substring Search Problem
热门文章
- Codeforces Round #646 (Div. 2) E. Tree Shuffling(树上dp)
- 2019牛客暑期多校训练营(第六场)J Upgrading Technology
- Gym 102263 ArabellaCPC 2019 J - Thanos Power (DP,数学)
- Linux系统编程【3.1】——编写ls命令
- 导出Excel的异常处理
- 多线程(四) AQS底层原理分析
- 2021 从零开始打造一个自己的 UI 组件库
- 从GitHub Jobs! 看技术发展趋势! 程序员进阶必备!
- Docker In Action
- 如何在 macOS 上进行滚动截屏