日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網(wǎng)站首頁 編程語言 正文

C++?boost?scoped_ptr智能指針詳解_C 語言

作者:無水先生 ? 更新時間: 2022-12-15 編程語言

一、智能指針-唯一所有者

boost::scoped_ptr 是一個智能指針,它是動態(tài)分配對象的唯一所有者。 boost::scoped_ptr 無法復制或移動。此智能指針在頭文件 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 是相同的,這里我們就不重復說明。需要的可以參考 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 寫入標準輸出。

使用 reset() 可以將新地址存儲在智能指針中。這樣,示例將包含數(shù)字 2 的新分配的 int 對象的地址傳遞給 p。通過調(diào)用 reset(),p 中當前引用的對象會被自動銷毀。

get() 返回錨定在智能指針中的對象的地址。該示例取消引用 get() 返回的地址以將 2 寫入標準輸出。

boost::scoped_ptr 重載操作符 operator bool。如果智能指針包含對對象的引用(也就是說,如果它不為空),則 operator bool 返回 true。該示例將 false 寫入標準輸出,因為 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.應用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

欄目分類
最近更新