当前位置:网站首页>初探智能指针之std::shared_ptr、std::unique_ptr
初探智能指针之std::shared_ptr、std::unique_ptr
2022-04-23 06:14:00 【CS生】
什么是智能指针、作用
智能指针用于确保当对象不再使用时对象可以被自动删除。
std::shared_ptr
几个指针能共享地指向同一个对象,当没有指针指向这个对象时,该对象会被删除(析构)。
// std::shared_ptr<MyTime> mt0 = new MyTime(0, 70); // error 类型不匹配
// MyTime* mt0 = std::make_shared(1, 70); // error 类型不匹配
std::shared_ptr<MyTime> mt1(new MyTime(10));
std::shared_ptr<MyTime> mt2 = mt1; // mt2和mt1指向同一个对象
auto mt1 = std::make_shared<MyTime>(1, 70); // C++17
std::unique_ptr
与std::shared_ptr不同,一个std::unique_ptr会指向一个对象且不允许其他指针指向。
std::unique_ptr<MyTime> mt1(new MyTime(10));
std::unique_ptr<MyTime> mt2 = std::make_unique<MyTime>(80); // C++17
但是,一个被std::unique_ptr指向的对象可以被move到另一个指针指向。
std::unique_ptr<MyTime> mt3 = std::move(mt1);
如何理解智能指针
智能指针的类型本质上是模板类,所以智能指针是对象,具有生命周期,当生命周期结束时,智能指针会销毁调用析构函数。
例如:std::shared_ptr的析构函数会对指向对象的指针数量减1,当没有指向该对象的指针时,对象内存销毁。
版权声明
本文为[CS生]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_36824130/article/details/123487614
边栏推荐
- 【点云系列】Multi-view Neural Human Rendering (NHR)
- 【点云系列】Learning Representations and Generative Models for 3D pointclouds
- torch_geometric学习一,MessagePassing
- [8] Assertion failed: dims.nbDims == 4 || dims.nbDims == 5
- UEFI学习01-ARM AARCH64编译、ArmPlatformPriPeiCore(SEC)
- Machine learning notes 1: learning ideas
- C language, a number guessing game
- 【动态规划】杨辉三角
- enforce fail at inline_container.cc:222
- PyTorch 17. GPU并发
猜你喜欢

PyTorch 10. 学习率

【點雲系列】SG-GAN: Adversarial Self-Attention GCN for Point Cloud Topological Parts Generation

F.pad 的妙用

MySQL数据库安装与配置详解

第3章 Pytorch神经网络工具箱

第4章 Pytorch数据处理工具箱

ArcGIS license server administrator cannot start the workaround

pth 转 onnx 时出现的 gather、unsqueeze 等算子
![[Point Cloud Series] SG - Gan: Adversarial Self - attachment GCN for Point Cloud Topological parts Generation](/img/1d/92aa044130d8bd86b9ea6c57dc8305.png)
[Point Cloud Series] SG - Gan: Adversarial Self - attachment GCN for Point Cloud Topological parts Generation

第5 章 机器学习基础
随机推荐
PyTorch 17. GPU并发
【点云系列】PnP-3D: A Plug-and-Play for 3D Point Clouds
如何利用qemu搭建SOC protoype:80行代码实现一个Cortex M4 模拟器
第5 章 机器学习基础
【点云系列】SG-GAN: Adversarial Self-Attention GCN for Point Cloud Topological Parts Generation
torch. mm() torch. sparse. mm() torch. bmm() torch. Mul () torch The difference between matmul()
EMMC/SD学习小记
Five methods are used to obtain the parameters and calculation of torch network model
PyTorch 10. 学习率
SSL / TLS application example
【51单片机交通灯仿真】
How keras saves and loads the keras model
x509解析
Gephi tutorial [1] installation
torch.where能否传递梯度
N states of prime number solution
Machine learning notes 1: learning ideas
Chapter 2 pytoch foundation 2
【動態規劃】不同路徑2
Data class of kotlin journey