下面随笔给出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.

c++中深层复制(浅层复制运行错误)成功运行-----sample的更多相关文章

  1. java数组对象的浅层复制与深层复制

    实际上,java中数组对象的浅层复制只是复制了对象的引用(参考),而深层复制的才是对象所代表的值.

  2. Java中的Clone机制(浅层复制)

    浅层复制代码: import java.util.*; class Int{ private int i; public Int(int ii){i = ii;} public void increm ...

  3. .net中String是引用类型还是值类型 以及 C#深层拷贝浅层拷贝

    http://www.cnblogs.com/yank/archive/2011/10/24/2204145.html http://www.cnblogs.com/zwq194/archive/20 ...

  4. iOS 浅复制和深复制的深层理解,含示例

    转载:https://www.zybuluo.com/MicroCai/note/50592 版权归 @MicroCai 所有 以下是正文: 浅复制就是指针拷贝:深复制就是内容拷贝. 集合的浅复制 ( ...

  5. java中浅层克隆和深层克隆

    1.浅复制与深复制概念 浅复制(浅克隆)     被复制对象的所有变量都含有与原来的对象相同的值,而所有的对其他对象的引用仍然指向原来的对象.换言之,浅复制仅仅复制所考虑的对象,而不复制它所引用的对象 ...

  6. JAVA中浅复制与深复制 - coolmist - ITeye技术网站

    body{ font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI& ...

  7. Java中引用的浅复制和深复制

    Java中除了基本类型int,char,double等的赋值是按照值传递之外,其余的类型和对象都是按照引用进行传递的. 下面来看一个关于引用的例子. package referenceCopy;// ...

  8. c#Winform程序调用app.config文件配置数据库连接字符串 SQL Server文章目录 浅谈SQL Server中统计对于查询的影响 有关索引的DMV SQL Server中的执行引擎入门 【译】表变量和临时表的比较 对于表列数据类型选择的一点思考 SQL Server复制入门(一)----复制简介 操作系统中的进程与线程

    c#Winform程序调用app.config文件配置数据库连接字符串 你新建winform项目的时候,会有一个app.config的配置文件,写在里面的<connectionStrings n ...

  9. Java中的浅复制和深复制 Cloneable clone

    先看一个简单案例 public class Test {     public static void main(String args[]) {         Student stu1 = new ...

随机推荐

  1. AtCoder Beginner Contest 179

    比赛链接:https://atcoder.jp/contests/abc179/tasks A - Plural Form 题意 给出一个由小写字母组成的单词,如果单词以 $s$ 结尾,在单词的末尾加 ...

  2. Java的awt包的使用实例和Java的一些提示框

    一.awt的一些组件 Label l1=new Label("姓名:"); //标签 Label l2=new Label("密码:"); TextField ...

  3. hdu4325 Flowers

    Problem Description As is known to all, the blooming time and duration varies between different kind ...

  4. Windows Server 2016 开启远程连接并延长过期时间

    按照下面文章配置,做完1.2步即可,其中协议号码填写 4954438 亲测有效! Server 2016默认远程桌面连接数是2个用户,如果多余两个用户进行远程桌面连接时,系统就会提示超过连接数,可以通 ...

  5. WPF 只读集合在 XAML 中的绑定(WPF:Binding for readonly collection in xaml)

    问题背景 某一天,我想做一个签到打卡的日历.基于 Calendar,想实现这个目标,于是找到了它的 SelectedDates 属性,用于标记签到过的日期. 问题来了. 基于MVVM模式,想将其在xa ...

  6. Yocto项目介绍及入门 -- 嵌入师工程师必备利器

    目录 写在前面 1. Yocto项目是什么 2. Yocto项目有什么用 3. 如何快速上手Yocto项目 4. 带你通过Yocto项目编译一个自定义镜像文件 写在前面 博主目前从事BMC工作,由于公 ...

  7. spark 一、编程指南

    总览 第一.每个spark 应用都有一个驱动程序去运行着主函数和再每个节点上的并行操作. spark提供了一个RDD(弹性分布式数据集)的数据集合,可以通过不同的节点并行操作运算,可以通过hdfs文件 ...

  8. OpenStack服务默认端口号

    在某些部署中,例如已设置限制性防火墙的部署,您可能需要手动配置防火墙以允许OpenStack服务流量. 要手动配置防火墙,您必须允许通过每个OpenStack服务使用的端口的流量.下表列出了每个Ope ...

  9. Zabbix 自动发现 & 自动注册

    自动发现 Zabbix 为用户提供了高效灵活的网络自动发现功能,有以下优点: 加快 Zabbix 部署 简化管理 无需过多管理,也能在快速变化的环境中使用 Zabbix Zabbix 网络发现基于以下 ...

  10. HDU 4511 小明系列故事——女友的考验 (AC自动机 + DP)题解

    题意:从 1 走到 n,要求所走路径不能出现给定的路径,求最短路 思路:因为要求不能出现给定路径,那么我可以求助ac自动机完成判断. 我们可以在build的时候标记哪些路径不能出现,显然下面这种表示后 ...