当前位置:网站首页>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
边栏推荐
猜你喜欢
Construction of promtail + Loki + grafana log monitoring system
漫画:什么是IaaS、PaaS、SaaS?
LVM and disk quota
阿里研发三面,面试官一套组合拳让我当场懵逼
欣旺达:HEV和BEV超快充拳头产品大规模出货
1959年高考数学真题
Knowledge points and examples of [seven input / output systems]
vim编辑器的实时操作
Sail soft implements a radio button, which can uniformly set the selection status of other radio buttons
Gartner 发布新兴技术研究:深入洞悉元宇宙
随机推荐
Execution plan calculation for different time types
MySql主从复制
Detailed explanation of Niuke - Gloves
Loggie source code analysis source file module backbone analysis
PyTorch:train模式与eval模式的那些坑
The first line and the last two lines are frozen when paging
Disk management and file system
About JMeter startup flash back
Creation of RAID disk array and RAID5
◰ GL shadow map core steps
Deepinv20 installation MariaDB
Kunteng full duplex digital wireless transceiver chip kt1605 / kt1606 / kt1607 / kt1608 is suitable for interphone scheme
File upload and download of robot framework
Sail soft implements a radio button, which can uniformly set the selection status of other radio buttons
Differences between MySQL BTREE index and hash index
LVM与磁盘配额
LVM and disk quota
Real time operation of vim editor
5分钟NLP:Text-To-Text Transfer Transformer (T5)统一的文本到文本任务模型
Modify the test case name generated by DDT