当前位置:网站首页>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
边栏推荐
- Scikit learn sklearn 0.18 official document Chinese version
- String function in MySQL
- C1 notes [task training chapter I]
- Classification of cifar100 data set based on convolutional neural network
- The method of changing a value in the array and a value in the object of wechat applet
- Go对文件操作
- undefined reference to `Nabo::NearestNeighbourSearch
- [UDS unified diagnostic service] IV. typical diagnostic service (6) - input / output control unit (0x2F)
- 2022 Jiangxi energy storage technology exhibition, China Battery exhibition, power battery exhibition and fuel cell Exhibition
- 解决报错max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
猜你喜欢

How to install jsonpath package

2022 Shanghai safety officer C certificate operation certificate examination question bank and simulation examination

2022 Jiangxi energy storage technology exhibition, China Battery exhibition, power battery exhibition and fuel cell Exhibition

极致体验,揭晓抖音背后的音视频技术

Go对文件操作

ArcGIS license error -15 solution
![[UDS unified diagnostic service] (Supplement) v. detailed explanation of ECU bootloader development points (1)](/img/74/bb173ca53d62304908ca80d3e96939.png)
[UDS unified diagnostic service] (Supplement) v. detailed explanation of ECU bootloader development points (1)

Random number generation of C #
Logic regression principle and code implementation

C# 网络相关操作
随机推荐
Flash operates on multiple databases
Tensorflow tensor introduction
2022 tea artist (primary) examination simulated 100 questions and simulated examination
C1 notes [task training part 2]
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
Map basemap Library
Implementation of object detection case based on SSD
列表的使用-增删改查
ArcGIS license error -15 solution
positioner
Calculation of fishing net road density
Nanotechnology + AI enabled proteomics | Luomi life technology completed nearly ten million US dollars of financing
C network related operations
The JS timestamp of wechat applet is converted to / 1000 seconds. After six hours and one day, this Friday option calculates the time
Arcpy adds fields and loop assignments to vector data
Summary of common server error codes
Identification verification code
Install pyshp Library
C# 的数据流加密与解密
Gaode map search, drag and drop query address