当前位置:网站首页>深拷贝和浅拷贝的区别
深拷贝和浅拷贝的区别
2022-04-23 05:46:00 【OceanKeeper1215】
默认拷贝构造可以完成对象的数据成员简单的赋值,这就是浅拷贝。
对象的数据资源是由指针指向的堆时,默认的拷贝构造函数只是将指针复制。
但是之后释放的时候,会释放两次,即同一个内存地址被释放了两次,会产生错误。
深拷贝是指,程序员自己重新写一份拷贝构造,在调用拷贝构造的时候,编译器优先调用程序员手写的拷贝构造,在程序员写的拷贝构造中,可以单独开辟一块内存空间用来保存数据,这就是深拷贝。
class Test
{
private:
int* p;
public:
Test(int x)
{
this->p=new int(x);
cout << "对象被创建" << endl;
}
~Test()
{
if (p != NULL)
{
delete p;
}
cout << "对象被释放" << endl;
}
int getX() { return *p; }
//深拷贝(拷贝构造函数)
Test(const Test& a)
{
this->p = new int(*a.p);
cout << "对象被创建" << endl;
}
//浅拷贝(拷贝构造函数)
//Test(const Test& a)
//{
// this->p = a.p;
// cout << "对象被创建" << endl;
//}
};
int main()
{
Test a(10);
//我们手动的写拷贝构造函数,C++编译器会调用我们手动写的
Test b = a;
return 0;
}
版权声明
本文为[OceanKeeper1215]所创,转载请带上原文链接,感谢
https://blog.csdn.net/OceanKeeper1215/article/details/120978438
边栏推荐
- Programming training
- Integration and induction of knowledge points of automatic control principle (Han min version)
- The problem that the page will refresh automatically after clicking the submit button on the form is solved
- Pytorch notes - observe dataloader & build lenet with torch to process cifar-10 complete code
- [leetcode 202] happy number
- JDBC operation transaction
- Troubleshooting of data deleted and reappeared problems
- 电机与拖动(戚金清版)学习整理
- Basic knowledge of network in cloud computing
- Motor and drive (Qi Jinqing Edition)
猜你喜欢

Mysql database foundation

C language file operation

Definition of C class and method

lambda expressions
![How to use comparative learning to do unsupervised - [cvpr22] training & [eccv20] image translation](/img/33/780b80693f70112eebc10941f7c134.png)
How to use comparative learning to do unsupervised - [cvpr22] training & [eccv20] image translation

Delete and truncate
![Unsupervised denoising - [tmi2022] ISCL: dependent self cooperative learning for unpaired image denoising](/img/cd/10793445e6867eeee613b6ba4b85cf.png)
Unsupervised denoising - [tmi2022] ISCL: dependent self cooperative learning for unpaired image denoising
![[untitled] database - limit the number of returned rows](/img/20/9a143e6972f1ce2eed5a3d11c3a46d.png)
[untitled] database - limit the number of returned rows

Algèbre linéaire chapitre 1 - déterminants

The bottom implementation principle of thread - static agent mode
随机推荐
Understanding and installing MySQL
Reading of denoising paper - [ridnet, iccv19] real image denoising with feature attention
[leetcode 202] happy number
Algèbre linéaire chapitre 2 - matrice et son fonctionnement
[leetcode 19] delete the penultimate node of the linked list
[leetcode 228] summary interval
Framework analysis 2 Source code - login authentication
Linear algebra Chapter 1 - determinant
PHP processing JSON_ Decode() parses JSON stringify
The problem that the page will refresh automatically after clicking the submit button on the form is solved
MySQL table constraints and table design
Qthread simple test understanding
Detection technology and principle
Preparedstatement prevents SQL injection
[transfer] MySQL: how many rows of data can InnoDB store in a B + tree?
[leetcode 59] spiral matrix II
2. Devops sonar installation
[leetcode 290] word rules
檢測技術與原理
[leetcode 6] zigzag transformation