当前位置:网站首页>The difference between deep copy and shallow copy
The difference between deep copy and shallow copy
2022-04-23 18:03:00 【OceanKeeper1215】
The default copy structure can complete the simple assignment of the data members of the object , This is a shallow copy .
When the data resource of the object is the heap pointed to by the pointer , The default copy constructor simply copies the pointer .
But then released , Will be released twice , That is, the same memory address is released twice , There will be mistakes .
Deep copy means , The programmer writes a copy of the structure himself , When calling the copy construct , The compiler preferentially calls the programmer's handwritten copy to construct , The programmer writes in a copy of , You can open up a separate memory space to store data , This is the deep copy .
class Test
{
private:
int* p;
public:
Test(int x)
{
this->p=new int(x);
cout << " Object created " << endl;
}
~Test()
{
if (p != NULL)
{
delete p;
}
cout << " Object is released " << endl;
}
int getX() { return *p; }
// Deep copy ( copy constructor )
Test(const Test& a)
{
this->p = new int(*a.p);
cout << " Object created " << endl;
}
// Shallow copy ( copy constructor )
//Test(const Test& a)
//{
// this->p = a.p;
// cout << " Object created " << endl;
//}
};
int main()
{
Test a(10);
// We manually write the copy constructor ,C++ The compiler will call what we wrote manually
Test b = a;
return 0;
}
版权声明
本文为[OceanKeeper1215]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230545105131.html
边栏推荐
- 2022 judgment questions and answers for operation of refrigeration and air conditioning equipment
- Map basemap Library
- [UDS unified diagnostic service] (Supplement) v. detailed explanation of ECU bootloader development points (1)
- Add animation to the picture under V-for timing
- word frequency count
- C byte array (byte []) and string are converted to each other
- MySQL 中的字符串函数
- Installation du docker redis
- Climbing watermelon video URL
- xlsxwriter. exceptions. Filecreateerror: [errno 13] permission denied
猜你喜欢
C network related operations
Install pyshp Library
Laser slam theory and practice of dark blue College Chapter 3 laser radar distortion removal exercise
YOLOv4剪枝【附代码】
2022 Jiangxi Photovoltaic Exhibition, China distributed Photovoltaic Exhibition, Nanchang solar energy utilization Exhibition
An example of linear regression based on tensorflow
Go's gin framework learning
Jenkspy package installation
Implementation of k8s redis one master multi slave dynamic capacity expansion
Clion installation tutorial
随机推荐
Tell the truth of TS
ArcGIS table to excel exceeds the upper limit, conversion failed
2022 Jiangxi energy storage technology exhibition, China Battery exhibition, power battery exhibition and fuel cell Exhibition
Go对文件操作
2022江西光伏展,中国分布式光伏展会,南昌太阳能利用展
C byte array (byte []) and string are converted to each other
2022 Shanghai safety officer C certificate operation certificate examination question bank and simulation examination
JS high frequency interview questions
QTableWidget使用讲解
Go语言JSON包使用
Special effects case collection: mouse planet small tail
Install pyshp Library
Halo 开源项目学习(二):实体类与数据表
MySQL_01_简单数据检索
587. Install fence / Sword finger offer II 014 Anagrams in strings
极致体验,揭晓抖音背后的音视频技术
Click Cancel to return to the previous page and modify the parameter value of the previous page, let pages = getcurrentpages() let prevpage = pages [pages. Length - 2] / / the data of the previous pag
Implementation of k8s redis one master multi slave dynamic capacity expansion
Read excel, int digital time to time
Theory and practice of laser slam in dark blue College - Chapter 2 (odometer calibration)