当前位置:网站首页>STL教程9-容器元素深拷贝和浅拷贝问题
STL教程9-容器元素深拷贝和浅拷贝问题
2022-07-03 10:45:00 【贪睡的蜗牛】
浅拷贝问题的一个案例
定义一个类
下面有个指针成员,这样容易出现浅拷贝问题
class Person {
public:
char* name;
int age;
public:
Person(const char * name,int age) {
this->name = new char[strlen(name) + 1];
strcpy(this->name, name);
this->age = age;
}
~Person() {
if (this->name != NULL) {
delete[] this->name;
}
}
};
然后定义一个类的对象,把它放到容器里面
int main() {
const char* a = "name";
Person p(a, 20);
vector<Person>v;
v.push_back(p);
}
然后直接报错
原因在于外面的对象和vector里面对象的name指针都指向了同一块内存,在程序结束后,外面对象开始析构,析构完成后vector里面的元素也要析构, 这样同一块内存就析构了两次,就会发生错误
解决方法
修改后的代码
class Person {
public:
char* name;
int age;
public:
Person(const char * name,int age) {
this->name = new char[strlen(name) + 1];
strcpy(this->name, name);
this->age = age;
}
Person(const Person& p) {
this->name = new char[strlen(p.name) + 1];
strcpy(this->name, p.name);
this->age = p.age;
}
Person& operator=(const Person& p) {
if (this->name != NULL) {
delete[] this->name;
}
this->name = new char[strlen(p.name) + 1];
strcpy(this->name, p.name);
this->age = p.age;
return *this;
}
~Person() {
if (this->name != NULL) {
delete[] this->name;
}
}
};
重新运行没有崩溃
STL容器共性机制(重要)
STL所有容器提供的都是值语意,而非引用语意。容器执行插入元素操作时,内部内部实施了拷贝工作,将我们要插入的元素在拷贝一份放入到容器汇总,而不是将原数据元素直接放进容器中,因此我们提供的元素必须能够被拷贝(如果有指针需要自己去构造)。
最后
这里使用strcpy会出现问题,报错
严重性 代码 说明 项目 文件 行 禁止显示状态
错误 C4996 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. map D:\store\server\leetcode\map\源.cpp 13
只需要在头部加上
#define _CRT_SECURE_NO_WARNINGS
边栏推荐
- 程序员的创业陷阱:接私活
- AMS series - application startup process
- Incremental database backup - DB incr DB full
- 一些常用术语
- [OBS] configFile in ini format of OBS
- Web安全总结
- How to mix embedded MCU, arm and DSP?
- Phpcms prompt message page Jump to showmessage
- FL Studio 20 unlimited trial fruit arranger Download
- 1. Hal driven development
猜你喜欢
Cadence background color setting
C language AES encryption and decryption
Mmc5603nj geomagnetic sensor (Compass example)
00后抛弃互联网: 毕业不想进大厂,要去搞最潮Web3
活动预告 | 直播行业“内卷”,以产品力拉动新的数据增长点
Event preview | the live broadcast industry "rolled in" to drive new data growth points with product power
Spl06-007 air pressure sensor (example of barometer)
Arctangent entropy: the latest SCI paper in July 2022
DS90UB949
Google Earth engine (GEE) - ghsl global population grid dataset 250 meter resolution
随机推荐
C语言二维数组
Excel快速跨表复制粘贴
Project management essence reading notes (VII)
How to: configure ClickOnce trust prompt behavior
Nestjs配置服务,配置Cookie和Session
Touch and screen automatic rotation debugging
LeetCode 46:全排列
Processes and threads
Execute kubectl on Tencent cloud container service node
How to become a senior digital IC Design Engineer (1-4) Verilog coding syntax: expression
[VTK] vtkWindowedSincPolyDataFilter 源码注释解读
[vtk] source code interpretation of vtkpolydatatoimagestencil
Key switch: press FN when pressing F1-F12
AOSP ~ NTP ( 网络时间协议 )
P3250 [hnoi2016] Network + [necpc2022] f.tree path tree section + segment tree maintenance heap
R语言使用aggregate函数计算dataframe数据分组聚合的均值(sum)、不设置na.rm计算的结果、如果分组中包含缺失值NA则计算结果也为NA
Google Earth engine (GEE) - ghsl global population grid dataset 250 meter resolution
Technical experts from large factories: how can engineers improve their communication skills?
基于turtlebot3实现SLAM建图及自主导航仿真
How to become a senior digital IC Design Engineer (1-3) Verilog coding syntax: Verilog behavior level, register transfer level, gate level (abstract level)