当前位置:网站首页>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
边栏推荐
- MySQL 中的字符串函数
- Fashion classification case based on keras
- positioner
- The ultimate experience, the audio and video technology behind the tiktok
- Detailed deployment of flask project
- Operation of 2022 mobile crane driver national question bank simulation examination platform
- 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
- Add animation to the picture under V-for timing
- Use of list - addition, deletion, modification and query
- Go的Gin框架学习
猜你喜欢

Examination question bank and online simulation examination of the third batch (main person in charge) of special operation certificate of safety officer a certificate in Guangdong Province in 2022

An example of linear regression based on tensorflow

MySQL_ 01_ Simple data retrieval

Operation of 2022 mobile crane driver national question bank simulation examination platform

C language loop structure program

Dock installation redis
![[UDS unified diagnostic service] (Supplement) v. detailed explanation of ECU bootloader development points (2)](/img/91/3272d5ad04cd1d8476c739546f4356.png)
[UDS unified diagnostic service] (Supplement) v. detailed explanation of ECU bootloader development points (2)
![Yolov4 pruning [with code]](/img/09/ea4376d52edb7e419ace2cb1e0356b.gif)
Yolov4 pruning [with code]

Auto.js 自定义对话框

cv_ Solution of mismatch between bridge and opencv
随机推荐
Timestamp to formatted date
Go语言JSON包使用
消费者灰度实现思路
云原生虚拟化:基于 Kubevirt 构建边缘计算实例
[UDS unified diagnostic service] IV. typical diagnostic service (6) - input / output control unit (0x2F)
Basic usage of crawler requests
idea中安装YapiUpload 插件将api接口上传到yapi文档上
Using files to save data (C language)
cartographer_ There is no problem compiling node, but running the bug that hangs directly
MySQL_ 01_ Simple data retrieval
Submit local warehouse and synchronize code cloud warehouse
C# 的数据流加密与解密
纳米技术+AI赋能蛋白质组学|珞米生命科技完成近千万美元融资
Build openstack platform
Calculation of fishing net road density
2022 tea artist (primary) examination simulated 100 questions and simulated examination
Re regular expression
MySQL 中的字符串函数
Nanotechnology + AI enabled proteomics | Luomi life technology completed nearly ten million US dollars of financing
.105Location