当前位置:网站首页>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
边栏推荐
- Esxi encapsulated network card driver
- Introduction to how to set up LAN
- 深入了解3D模型相关知识(建模、材质贴图、UV、法线),置换贴图、凹凸贴图与法线贴图的区别
- G008-hwy-cc-estor-04 Huawei Dorado V6 storage simulator configuration
- About JMeter startup flash back
- Pseudo Distributed installation spark
- NVIDIA显卡驱动报错
- JIRA screenshot
- The solution of not displaying a whole line when the total value needs to be set to 0 in sail software
- 浅谈 NFT项目的价值、破发、收割之争
猜你喜欢

Zhongang Mining: Fluorite Flotation Process

建站常用软件PhpStudy V8.1图文安装教程(Windows版)超详细

Use itextpdf to intercept the page to page of PDF document and divide it into pieces

Dlib of face recognition framework

Pycham connects to the remote server and realizes remote debugging

昆腾全双工数字无线收发芯片KT1605/KT1606/KT1607/KT1608适用对讲机方案

Idea of batch manufacturing test data, with source code

阿里研发三面,面试官一套组合拳让我当场懵逼

Gartner announces emerging technology research: insight into the meta universe

Construction of promtail + Loki + grafana log monitoring system
随机推荐
昆腾全双工数字无线收发芯片KT1605/KT1606/KT1607/KT1608适用对讲机方案
Dancenn: overview of byte self-developed 100 billion scale file metadata storage system
logback的配置文件加载顺序
关于 background-image 渐变gradient()那些事!
计算饼状图百分比
众昂矿业:萤石浮选工艺
Calculate pie chart percentage
Disk management and file system
Set cell filling and ranking method according to the size of the value in the soft report
Encapsulating the logging module
ACL 2022 | dialogved: a pre trained implicit variable encoding decoding model for dialogue reply generation
vim编辑器的实时操作
博士申请 | 厦门大学信息学院郭诗辉老师团队招收全奖博士/博后/实习生
Nacos 详解,有点东西
Gartner publie une étude sur les nouvelles technologies: un aperçu du métacosme
Introduction notes to PHP zero Foundation (13): array related functions
浅谈 NFT项目的价值、破发、收割之争
New project of OMNeT learning
Selenium IDE and XPath installation of chrome plug-in
PHP 零基础入门笔记(13):数组相关函数