網站首頁 編程語言 正文
shared_ptr
shared_ptr 是一個標準的共享所有權的智能指針,允許多個指針指向同一個對象,定義在 memory 文件中,命名空間為 std。shared_ptr最初實現于Boost庫中,后由 C++11 引入到 C++ STL。shared_ptr 利用引用計數的方式實現了對所管理的對象的所有權的分享,即允許多個 shared_ptr 共同管理同一個對象。
std::shared_ptr<int> sp1 = new int(); // shared count = 1, weak count = 0
std::shared_ptr<int> sp2(sp1); // shared count = 2, weak count = 0
std::shared_ptr<int> sp3 = sp2; // shared count = 3, weak count = 0
std::weak_ptr<int> wp1(sp1); // shared count = 3, weak count = 1
std::weak_ptr<int> wp2(wp1); // shared count = 3, weak count = 2
std::weak_ptr<int> wp3 = wp2; // shared count = 3, weak count = 3
shared_ptr weak_ptr 使用 reset 或者指向另一個 managed object導致 shared count或weak count相應的減一。
1.類繼承中使用shared_ptr
class Base {};
class Derived : public Base {};
......
shared_ptr<Derived> dp1(new Derived);
shared_ptr<Base> bp1 = dp1;
shared_ptr<Base> bp2(dp1);
shared_ptr<Base> bp3(new Derived);
2.casting shared_ptr
shared_ptr<Base> base_ptr (new Base);
shared_ptr<Derived> derived_ptr;
// if static_cast<Derived *>(base_ptr.get()) is valid, then the following is valid:
derived_ptr = static_pointer_cast<Derived>(base_ptr);
3.make_shared
使用shared_ptr = new int(),會導致兩次內存分配:int對象的內存分配跟shared_ptr內部的 manager object 一次內存分配。make_shared 對此進行了優化,一次性分配 int + manager object 內存空間大小。
make_shared 用法:
shared_ptr<Thing> p (make_shared<Thing>(42, "I'm a Thing!"));
shared_ptr<Base> bp(make_shared<Derived1>()); // shared_ptr中的 template參數與make_shared中的tmeplate參數可以不一樣(繼承關系)
使用 weak_ptr
void do_it(weak_ptr<Thing> wp){
shared_ptr<Thing> sp = wp.lock(); // get shared_ptr from weak_ptr
if(sp)
sp->defrangulate(); // tell the Thing to do something
else
cout << "The Thing is gone!" << endl;
}
也可以直接從weak_ptr構建shared_ptr,這個時間如果weak_ptr過期(通過 weak_ptr::expired() 可以查詢),則拋出異常:
void do_it(weak_ptr<Thing> wp){
shared_ptr<Thing> sp(wp); // construct shared_ptr from weak_ptr
// exception thrown if wp is expired, so if here, sp is good to go
sp->defrangulate(); // tell the Thing to do something
}
原文鏈接:https://www.cnblogs.com/vaughnhuang/p/16511072.html
相關推薦
- 2024-02-16 SpringBoot 全局異常處理
- 2022-12-24 C#?Math中常用數學運算的示例詳解_C#教程
- 2022-08-02 Python分支語句常見的使用方法_python
- 2022-03-15 SpringCloud 使用OpenFeign調用其他服務的時候feign.RetryableExc
- 2023-03-27 詳解C/C++高精度(加減乘除)算法中的壓位優化_C 語言
- 2023-04-02 Python中的常見數據集打亂方法_python
- 2024-03-06 Springboot實現緩存預熱
- 2022-03-31 Python基礎中的列表你了解嗎_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支