当前位置:网站首页>STD:: shared of smart pointer_ ptr、std::unique_ ptr
STD:: shared of smart pointer_ ptr、std::unique_ ptr
2022-04-23 12:59:00 【CS student】
What is a smart pointer 、 effect
Smart pointers are used to ensure that objects can be automatically deleted when they are no longer in use .
std::shared_ptr
Several pointers can point to the same object in a shared way , When there is no pointer to this object , The object is deleted ( destructor ).
// std::shared_ptr<MyTime> mt0 = new MyTime(0, 70); // error Type mismatch
// MyTime* mt0 = std::make_shared(1, 70); // error Type mismatch
std::shared_ptr<MyTime> mt1(new MyTime(10));
std::shared_ptr<MyTime> mt2 = mt1; // mt2 and mt1 Point to the same object
auto mt1 = std::make_shared<MyTime>(1, 70); // C++17
std::unique_ptr
And std::shared_ptr Different , One std::unique_ptr Will point to an object and no other pointers are allowed to point to .
std::unique_ptr<MyTime> mt1(new MyTime(10));
std::unique_ptr<MyTime> mt2 = std::make_unique<MyTime>(80); // C++17
however , One being std::unique_ptr The object pointed to can be move To another pointer pointing to .
std::unique_ptr<MyTime> mt3 = std::move(mt1);
How to understand smart pointers
The type of smart pointer is essentially a template class , So smart pointers are objects , With life cycle , At the end of the life cycle , The smart pointer destroys the call destructor .
for example :std::shared_ptr The destructor of will reduce the number of pointers to the object 1, When there is no pointer to the object , Object memory destroyed .
版权声明
本文为[CS student]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230613588872.html
边栏推荐
- Servlet监听器&过滤器介绍
- Free and open source agricultural Internet of things cloud platform (version: 3.0.1)
- V-model binding value in El select, data echo only displays value, not label
- BUUCTF WEB [BUUCTF 2018]Online Tool
- CVPR 2022&NTIRE 2022|首个用于高光谱图像重建的 Transformer
- What are the forms of attack and tampering on the home page of the website
- jmeter操作redis
- Golang realizes regular matching: the password contains at least one digit, letter and special character, and the length is 8-16
- The El table horizontal scroll bar is fixed at the bottom of the visual window
- No idle servers? Import OVF image to quickly experience smartx super fusion community version
猜你喜欢
Record a website for querying compatibility, string Replaceall() compatibility error
精度、速度完美平衡,最新图像分割SOTA模型重磅发布!!!
Record some NPM related problems (messy records)
PC starts multiple wechat at one time
4. DRF permission & access frequency & filtering & sorting
Sort out several uses of network IP agent
Calculate the past date and days online, and calculate the number of live days
Can I take the CPDA data analyst certificate for 0 foundation
梳理網絡IP代理的幾大用途
数据库中的日期时间类型
随机推荐
STM32 project transplantation: transplantation between chip projects of different models: Ze to C8
Mysql8 installation
使用Source Insight查看编辑源代码
CGC: contractual graph clustering for community detection and tracking
Get the punch in record of nailing attendance machine
云原生KubeSphere部署Mysql
STM32 control stepper motor (ULN2003 + 28byj)
leetcode-791. 自定义字符串排序
Trier les principales utilisations de l'Agent IP réseau
拥抱机器视觉新蓝海,冀为好望开启数字经济发展新“冀”遇
C, calculation code of parameter points of two-dimensional Bezier curve
Go language mapping operation
SSM框架系列——注解开发day2-2
After the data of El table is updated, the data in the page is not updated this$ Forceupdate() has no effect
将新增和编辑的数据同步更新到列表
Summary of JVM knowledge points - continuously updated
Connect orcale
22. Bracket generation
Subscribe to Alibaba demo send business messages
Buuctf Web [bjdctf2020] zjctf, but so