当前位置:网站首页>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
边栏推荐
- CVPR 2022&NTIRE 2022|首个用于高光谱图像重建的 Transformer
- 22. Bracket generation
- 内核错误: No rule to make target ‘debian/canonical-certs.pem‘, needed by ‘certs/x509_certificate_list‘
- 云原生KubeSphere部署Redis
- Remote sensing image classification and recognition system based on convolutional neural network
- [daily question] chessboard question
- SSM framework series - data source configuration day2-1
- JDBC connection pool
- PC starts multiple wechat at one time
- Van uploader upload picture implementation process, using native input to upload pictures
猜你喜欢

Unlock openharmony technology day! The annual event is about to open!

安装nngraph

5 free audio material websites, recommended collection

Important knowledge of transport layer (interview, retest, final)

How to click an object to play an animation

STM32 control stepper motor (ULN2003 + 28byj)

Record some NPM related problems (messy records)

PC starts multiple wechat at one time

Learning materials

将新增和编辑的数据同步更新到列表
随机推荐
No idle servers? Import OVF image to quickly experience smartx super fusion community version
Jupiter notebook installation
Van uploader upload picture implementation process, using native input to upload pictures
产品开发都应该知道的8个网站,增强工作体验
Importerror after tensorflow installation: DLL load failed: the specified module cannot be found, and the domestic installation is slow
4.22学习记录(你一天只做了水题是吗)
Packet capturing and sorting -- TCP protocol [8]
PC starts multiple wechat at one time
云原生KubeSphere部署Mysql
【csnote】ER图
云原生KubeSphere部署Redis
Buuctf Web [gxyctf2019] no dolls
Go iris framework implements multi service Demo: start (listen to port 8084) service 2 through the interface in service 1 (listen to port 8083)
At instruction of nbiot
BaseRecyclerViewAdapterHelper 实现下拉刷新和上拉加载
使用Source Insight查看编辑源代码
Free and open source charging pile Internet of things cloud platform
JDBC connection pool
The El table horizontal scroll bar is fixed at the bottom of the visual window
Golang implements a five insurance and one gold calculator with web interface