当前位置:网站首页>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
边栏推荐
- Halo 开源项目学习(二):实体类与数据表
- Gaode map search, drag and drop query address
- Crawl lottery data
- Stanford machine learning course summary
- Go's gin framework learning
- Crawl the product data of cicada mother data platform
- C language loop structure program
- Install pyshp Library
- Summary of common server error codes
- ArcGIS license error -15 solution
猜你喜欢

Data stream encryption and decryption of C

JS get link? The following parameter name or value, according to the URL? Judge the parameters after

Nat commun | current progress and open challenges of applied deep learning in Bioscience

Go语言JSON包使用

C# 网络相关操作

C1小笔记【任务训练篇一】

Halo 开源项目学习(二):实体类与数据表

Dock installation redis

The JS timestamp of wechat applet is converted to / 1000 seconds. After six hours and one day, this Friday option calculates the time

Batch export ArcGIS attribute table
随机推荐
C1 notes [task training part 2]
2022 tea artist (primary) examination simulated 100 questions and simulated examination
Queue solving Joseph problem
Crawl the product data of Xiaomi Youpin app
Gobang game based on pyGame Library
Crawler for querying nicknames and avatars based on qqwebapi
QTableWidget使用讲解
纳米技术+AI赋能蛋白质组学|珞米生命科技完成近千万美元融资
I / O multiplexing and its related details
Docker 安裝 Redis
C#的随机数生成
Stanford machine learning course summary
The ultimate experience, the audio and video technology behind the tiktok
读取excel,int 数字时间转时间
Scikit learn sklearn 0.18 official document Chinese version
Realsense selection comparison d455 d435i d415 t265 3D hardware comparison
mysql自动启动设置用Systemctl start mysqld启动
Basic usage of crawler requests
Random number generation of C #
Implementation of object detection case based on SSD