当前位置:网站首页>拷贝构造函数 浅拷贝与深拷贝
拷贝构造函数 浅拷贝与深拷贝
2022-04-23 16:42:00 【心 灵 骇 客】
目录
拷贝构造函数
上一期中我们讲述了构造函数的相关内容,谈到构造函数在形式上有几种分类,即带参数的、不带参数的以及参数列表初始化的,还有一种传引用的构造函数,称为拷贝构造函数,顾名思义,就是起到拷贝的功能,通过一个已经存在的类对象的相关数据初始化一个新的类对象。
我们还是用Point这个类作为例子进行讲解,拷贝构造函数就是参数类型为引用类型的构造函数。
class Point
{
double x,y;
public:
Point(Point & point);
};
浅拷贝
所有的类都有自己的拷贝构造函数,如果程序员自己没有写拷贝构造函数,那么系统会默认生成一个缺省的拷贝构造函数,它采取逐位复制的方法进行对象拷贝,又称为浅拷贝。
我们自己写一个Point类的浅拷贝的拷贝构造函数作为例子:
Point::Point(Point & point)
{
x=point.x;
y=point.y;
}
这就是浅拷贝,即逐位复制。看样子没什么毛病,为什么说它浅呢?
实际上,如果数据成员都是基本数据类型,浅拷贝是没有什么问题的。
但是,当一个类的数据成员中含有指针的时候,浅拷贝就会出事。
我们把Point类修改一下,增加一个指针指向Point类,为了安全,我们把这个指针初始化为NULL:
class Point
{
double x,y;
Point * p=NULL;
public:
Point(Point & point);
};
然后我们用浅拷贝:
Point::Point(Point & point)
{
x=point.x;
y=point.y;
p=point.p;
}
首先x和y是没有问题的,指针p的值虽然是相同了,但是我们的目的是想让指针的值相同吗,显然不是,是想让它们指向的内存单元具有相同的值,如果采用这样的浅拷贝,那么这两个指针指向的是同一块内存,有同学可能会说,这好像没有问题啊,这不也成功复制了吗?问题在于一旦我们修改其中一个类对象的p指向的内容,那么另一个类对象的数据也会改变,因为它们指向的是同一块内存。因此,我们在这种情况下需要用深拷贝。
深拷贝
通过上面的分析,我们知道浅拷贝的问题在于,这两个指针指向了同一块内存,所以只要给生成的类对象一块新内存空间就可以解决问题了:
Point::Point(Point & point)
{
x=point.x;
y=point.y;
if(point.p)
{
p=new Point;
p->x=point.p->x;
p->y=point.p->y;
}
}
在这里你可以看到,我们是首先判断p是不是空指针,不是空指针我们再复制,这里非常重要,如果没有这个判断,一旦p是空指针,而我们还是继续操作,引发系统不可预知的错误。
版权声明
本文为[心 灵 骇 客]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_62264287/article/details/124358974
边栏推荐
- Force buckle - 198 raid homes and plunder houses
- Camtasia2022软件新增功能介绍
- Esxi encapsulated network card driver
- 蓝桥杯省一之路06——第十二届省赛真题第二场
- Use case execution of robot framework
- G008-hwy-cc-estor-04 Huawei Dorado V6 storage simulator configuration
- UWA Pipeline 功能详解|可视化配置自动测试
- Summary according to classification in sail software
- DanceNN:字节自研千亿级规模文件元数据存储系统概述
- ◰GL-阴影贴图核心步骤
猜你喜欢
∑GL-透视投影矩阵的推导
100 deep learning cases | day 41 - convolutional neural network (CNN): urbansound 8K audio classification (speech recognition)
ACL 2022 | DialogVED:用于对话回复生成的预训练隐变量编码-解码模型
STM32__03—初识定时器
How magical is the unsafe class used by all major frameworks?
人脸识别框架之dlib
You need to know about cloud disaster recovery
面试百分百问到的进程,你究竟了解多少
PyTorch:train模式与eval模式的那些坑
昆腾全双工数字无线收发芯片KT1605/KT1606/KT1607/KT1608适用对讲机方案
随机推荐
Sail soft implements a radio button, which can uniformly set the selection status of other radio buttons
04 Lua operator
Take according to the actual situation, classify and summarize once every three levels, and see the figure to know the demand
Query the data from 2013 to 2021, and only query the data from 2020. The solution to this problem is carried out
蓝桥杯省一之路06——第十二届省赛真题第二场
Hypermotion cloud migration completes Alibaba cloud proprietary cloud product ecological integration certification
English | day15, 16 x sentence true research daily sentence (clause disconnection, modification)
Installation and management procedures
Disk management and file system
Modify the test case name generated by DDT
PyMySQL
欣旺达:HEV和BEV超快充拳头产品大规模出货
如何建立 TikTok用户信任并拉动粉丝增长
On the security of key passing and digital signature
聊一聊浏览器缓存控制
LVM and disk quota
Oracle data pump usage
Custom implementation of Baidu image recognition (instead of aipocr)
Detailed explanation of UWA pipeline function | visual configuration automatic test
Set the color change of interlaced lines in cells in the sail software and the font becomes larger and red when the number is greater than 100