網(wǎng)站首頁 編程語言 正文
一、智能指針-唯一所有者
boost::scoped_ptr 是一個智能指針,它是動態(tài)分配對象的唯一所有者。 boost::scoped_ptr 無法復(fù)制或移動。此智能指針在頭文件 boost/scoped_ptr.hpp 中定義。
二、接口類分析
scoped_array 分析
scoped_array 的類部分原始代碼如下:
template<class T> class scoped_array // noncopyable { private: T * px; scoped_array(scoped_array const &); scoped_array & operator=(scoped_array const &); typedef scoped_array<T> this_type; void operator==( scoped_array const& ) const; void operator!=( scoped_array const& ) const; public: typedef T element_type; explicit scoped_array( T * p = 0 ) BOOST_SP_NOEXCEPT : px( p ) { } ~scoped_array() // never throws { boost::checked_array_delete( px ); } void reset(T * p = 0) // never throws (but has a BOOST_ASSERT in it, so not marked with BOOST_NOEXCEPT) { BOOST_ASSERT( p == 0 || p != px ); // catch self-reset errors this_type(p).swap(*this); } T & operator[](std::ptrdiff_t i) const // never throws (but has a BOOST_ASSERT in it, so not marked with BOOST_NOEXCEPT) { BOOST_ASSERT( px != 0 ); BOOST_ASSERT( i >= 0 ); return px[i]; } T * get() const BOOST_NOEXCEPT { return px; } // implicit conversion to "bool" #include <boost/smart_ptr/detail/operator_bool.hpp> void swap(scoped_array & b) BOOST_NOEXCEPT { T * tmp = b.px; b.px = px; px = tmp; } };
從源碼上可以看出scoped_array 的接口和功能幾乎與scoped_ptr 是相同的,這里我們就不重復(fù)說明。需要的可以參考 boost::scoped_ptr智能指針。
示例 1.1.如何用boost::scoped_ptr
#include <boost/scoped_ptr.hpp> #include <iostream> int main() { boost::scoped_ptr<int> p{new int{1}}; std::cout << *p << '\n'; p.reset(new int{2}); std::cout << *p.get() << '\n'; p.reset(); std::cout << std::boolalpha << static_cast<bool>(p) << '\n'; }
參考結(jié)果
boost::scoped_ptr 類型的智能指針不能轉(zhuǎn)移對象的所有權(quán)。使用地址初始化后,動態(tài)分配的對象會在執(zhí)行析構(gòu)函數(shù)或調(diào)用成員函數(shù) reset() 時釋放。
示例 1.1 使用類型為 boost::scoped_ptr<int> 的智能指針 p。 p 使用指向存儲數(shù)字 1 的動態(tài)分配對象的指針進行初始化。通過運算符 *,p 被取出引用并將 1 寫入標(biāo)準(zhǔn)輸出。
使用 reset() 可以將新地址存儲在智能指針中。這樣,示例將包含數(shù)字 2 的新分配的 int 對象的地址傳遞給 p。通過調(diào)用 reset(),p 中當(dāng)前引用的對象會被自動銷毀。
get() 返回錨定在智能指針中的對象的地址。該示例取消引用 get() 返回的地址以將 2 寫入標(biāo)準(zhǔn)輸出。
boost::scoped_ptr 重載操作符 operator bool。如果智能指針包含對對象的引用(也就是說,如果它不為空),則 operator bool 返回 true。該示例將 false 寫入標(biāo)準(zhǔn)輸出,因為 p 已通過調(diào)用 reset() 重置。
boost::scoped_ptr 的析構(gòu)函數(shù)通過 delete 釋放引用的對象。這就是為什么 boost::scoped_ptr 不能用動態(tài)分配的數(shù)組的地址來初始化,必須用 delete[] 來釋放。對于數(shù)組,Boost.SmartPointers 提供了 boost::scoped_array 類。
示例 1.2.應(yīng)用boost::scoped_array
#include <boost/scoped_array.hpp> int main() { boost::scoped_array<int> p{new int[2]}; *p.get() = 1; p[1] = 2; p.reset(new int[3]); }
智能指針 boost::scoped_array 的使用與 boost::scoped_ptr 類似。關(guān)鍵的區(qū)別在于 boost::scoped_array 的析構(gòu)函數(shù)使用操作符 delete[] 來釋放包含的對象。由于此運算符僅適用于數(shù)組,因此 boost::scoped_array 必須使用動態(tài)分配的數(shù)組的地址進行初始化。
boost::scoped_array 在 boost/scoped_array.hpp 中定義。
boost::scoped_array 為 operator[] 和 operator bool 提供重載。使用 operator[],可以訪問數(shù)組的特定元素。因此,boost::scoped_array 類型的對象的行為類似于它所擁有的數(shù)組。示例 1.2 將數(shù)字 2 保存為 p 引用的數(shù)組中的第二個元素。
與 boost::scoped_ptr 一樣,提供了成員函數(shù) get() 和 reset() 來檢索和重新初始化所包含對象的地址。
原文鏈接:https://yamagota.blog.csdn.net/article/details/127045372
相關(guān)推薦
- 2022-05-20 springCloud_Nacos服務(wù)搭建
- 2023-11-12 jetson nano報錯Cannot allocate memory的問題——解決辦法
- 2022-11-09 WPF使用WrapPanel實現(xiàn)虛擬化效果_C#教程
- 2022-06-11 C#把DataTable導(dǎo)出為Excel文件_C#教程
- 2022-11-14 Go語言文件讀寫操作案例詳解_Golang
- 2023-02-25 深入理解go?reflect反射慢的原因_Golang
- 2022-05-23 React中setState同步異步場景的使用_React
- 2022-12-23 Python?tensorflow與pytorch的浮點運算數(shù)如何計算_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支