当前位置:网站首页>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
边栏推荐
- 梳理网络IP代理的几大用途
- Idea的src子文件下无法创建servlet
- Use compressorjs to compress pictures, optimize functions, and compress pictures in all formats
- 梳理網絡IP代理的幾大用途
- Timing role in the project
- Important knowledge of transport layer (interview, retest, final)
- 22. 括号生成
- Golang implements a five insurance and one gold calculator with web interface
- 21 days learning mongodb notes
- ZigBee CC2530 minimum system and register configuration (1)
猜你喜欢
SSM framework series - data source configuration day2-1
What are the forms of attack and tampering on the home page of the website
V-model binding value in El select, data echo only displays value, not label
Use compressorjs to compress pictures, optimize functions, and compress pictures in all formats
HQL find the maximum value in a range
STM32 is connected to the motor drive, the DuPont line supplies power, and then the back burning problem
CGC: contractual graph clustering for community detection and tracking
Realize several "Postures" in which a box is horizontally and vertically centered in the parent box
MySQL —— 16、索引的数据结构
云原生KubeSphere部署Mysql
随机推荐
Go iris framework implements multi service Demo: start (listen to port 8084) service 2 through the interface in service 1 (listen to port 8083)
C, calculation code of parameter points of two-dimensional Bezier curve
How to click an object to play an animation
Read the data in Presto through sparksql and save it to Clickhouse
Introduction to kubernetes
风尚云网学习-h5的input:type属性的image属性
CGC: contractual graph clustering for community detection and tracking
Remote access to raspberry pie at home (Part 1)
Keyword interpretation and some APIs in RT thread
Customize classloader and implement hot deployment - use loadclass
Luogu p5540 [balkanoi2011] timeismoney | minimum product spanning tree problem solution
Free and open source intelligent charging pile SaaS cloud platform of Internet of things
数据库中的日期时间类型
No idle servers? Import OVF image to quickly experience smartx super fusion community version
Pytorch: a pit about the implementation of gradreverselayer
World Book Day: I'd like to recommend these books
Kubernets Getting started tutoriel
精度、速度完美平衡,最新图像分割SOTA模型重磅发布!!!
mysql8安装
leetcode:437. 路径总和 III【dfs 选还是不选?】