当前位置:网站首页>Copy constructor shallow copy and deep copy
Copy constructor shallow copy and deep copy
2022-04-23 16:44:00 【Mind hacker】
Catalog
copy constructor
In the last installment, we talked about constructors , When it comes to constructors, there are several formal classifications , I.e. with parameters 、 Without parameters and parameter list initialization , There is also a constructor for passing references , Called a copy constructor , seeing the name of a thing one thinks of its function , Is to copy , Initialize a new class object through the relevant data of an existing class object .
We still use Point This class is explained as an example , A copy constructor is a constructor whose parameter type is a reference type .
class Point
{
double x,y;
public:
Point(Point & point);
};
Shallow copy
All classes have their own copy constructors , If the programmer doesn't write a copy constructor himself , Then the system will generate a default copy constructor by default , It adopts the method of bit by bit replication to copy objects , Also known as shallow copy .
Let's write one ourselves Point Class as an example :
Point::Point(Point & point)
{
x=point.x;
y=point.y;
}
This is a shallow copy , Copy bit by bit . There seems to be nothing wrong , Why is it shallow ?
actually , If all data members are basic data types , There is no problem with shallow copies .
however , When the data member of a class contains a pointer , Shallow copy will have an accident .
We put Point Class change , Add a pointer to Point class , For the sake of safety , We initialize this pointer to NULL:
class Point
{
double x,y;
Point * p=NULL;
public:
Point(Point & point);
};
Then we use a light copy :
Point::Point(Point & point)
{
x=point.x;
y=point.y;
p=point.p;
}
First x and y There is no problem , The pointer p Although the value of is the same , But our goal is to make the value of the pointer the same , Obviously not , You want the memory units they point to to to have the same value , If you use such a shallow copy , Then these two pointers point to the same block of memory , Some of you might say , There seems to be no problem , Didn't this copy successfully ? The problem is that once we modify one of the class objects p Point to , Then the data of another class object will also change , Because they point to the same piece of memory . therefore , In this case, we need to use deep copy .
Deep copy
Through the above analysis , We know that the problem with shallow copy is , These two pointers point to the same block of memory , So just give the generated class object a new memory space to solve the problem :
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;
}
}
Here you can see , We are the first to judge p Is it a null pointer , It's not a null pointer. Let's copy , This is very important , Without this judgment , once p Is a null pointer , And we continue to operate , Cause unpredictable errors in the system .
版权声明
本文为[Mind hacker]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231642096531.html
边栏推荐
- Execution plan calculation for different time types
- NVIDIA显卡驱动报错
- Dancenn: overview of byte self-developed 100 billion scale file metadata storage system
- ACL 2022 | dialogved: a pre trained implicit variable encoding decoding model for dialogue reply generation
- ByteVCharts可视化图表库,你想要的我都有
- DDT + Excel for interface test
- loggie 源码分析 source file 模块主干分析
- Findstr is not an internal or external command workaround
- 人脸识别框架之dlib
- 面试百分百问到的进程,你究竟了解多少
猜你喜欢
Use case execution of robot framework
OMNeT学习之新建工程
Detailed explanation of information abstract, digital signature, digital certificate, symmetric encryption and asymmetric encryption
DanceNN:字节自研千亿级规模文件元数据存储系统概述
[pyGame games] how did angry birds, a mobile game that became popular all over the world 10 years ago, dominate the list? Classic return
Kunteng full duplex digital wireless transceiver chip kt1605 / kt1606 / kt1607 / kt1608 is suitable for interphone scheme
Gartner 发布新兴技术研究:深入洞悉元宇宙
RAID磁盘阵列与RAID5的创建
100 deep learning cases | day 41 - convolutional neural network (CNN): urbansound 8K audio classification (speech recognition)
PyMySQL
随机推荐
Construction of promtail + Loki + grafana log monitoring system
Selenium IDE and XPath installation of chrome plug-in
Take according to the actual situation, classify and summarize once every three levels, and see the figure to know the demand
伪分布安装spark
Detailed explanation of information abstract, digital signature, digital certificate, symmetric encryption and asymmetric encryption
Loggie source code analysis source file module backbone analysis
Esxi encapsulated network card driver
各大框架都在使用的Unsafe类,到底有多神奇?
Regular filtering of Intranet addresses and segments
How does flash cache data in memory?
STM32__03—初识定时器
Loading order of logback configuration file
On the value, breaking and harvest of NFT project
Gartner 发布新兴技术研究:深入洞悉元宇宙
On the security of key passing and digital signature
七朋元视界可信元宇宙社交体系满足多元化的消费以及社交需求
Public variables of robotframework
MySQL personal learning summary
Report FCRA test question set and answers (11 wrong questions)
5-minute NLP: text to text transfer transformer (T5) unified text to text task model