当前位置:网站首页>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
边栏推荐
- Install nngraph
- At instruction of nbiot
- Bert base Chinese Download (SMART)
- Read the data in Presto through sparksql and save it to Clickhouse
- Buuctf Web [gxyctf2019] no dolls
- C, calculation code of parameter points of two-dimensional Bezier curve
- mysql8安装
- 内核错误: No rule to make target ‘debian/canonical-certs.pem‘, needed by ‘certs/x509_certificate_list‘
- MySQL supports IP access
- Summary of JVM knowledge points - continuously updated
猜你喜欢

Introduction to kubernetes

云原生KubeSphere部署Mysql

Learning materials

XinChaCha Trust SSL Organization Validated

CGC: contractual graph clustering for community detection and tracking

Record the problems encountered in using v-print

SSM框架系列——数据源配置day2-1

Use compressorjs to compress pictures, optimize functions, and compress pictures in all formats

Synchronously update the newly added and edited data to the list
![[vulnhub range] - DC2](/img/b7/c34f69a231dad653b8a912f1f36bef.png)
[vulnhub range] - DC2
随机推荐
Introduction to servlet listener & filter
梳理網絡IP代理的幾大用途
PHP generates JSON to process Chinese
MySQL —— 16、索引的数据结构
22. 括号生成
教你快速开发一个 狼人杀微信小程序(附源码)
Read the data in Presto through sparksql and save it to Clickhouse
Introducing vant components on demand
Use source insight to view and edit source code
Unable to create servlet under SRC subfile of idea
Use of Presto date function
[csnote] ER diagram
1130 - host XXX is not allowed to connect to this MySQL server error in Navicat remote connection database
世界读书日:我想推荐这几本书
4. DRF permission & access frequency & filtering & sorting
mysql8安装
Navicat远程连接数据库 出现 1130- Host xxx is not allowed to connect to this MySQL server错误
After the data of El table is updated, the data in the page is not updated this$ Forceupdate() has no effect
The continuous construction of the Internet industry platform is not only able to collect traffic
Get the punch in record of nailing attendance machine