Boost::smart_Ptr makes up for AutoPtr

來源:互聯網
上載者:User

Boost::smart_Ptr
   
    我們學習C++都知道智能指標,例如STL中的std::auto_ptr,但是為什麼要使用智能指標,使用它能帶給我們什麼好處呢?

最簡單的使用智能指標可以不會因為忘記delete指標而造成記憶體泄露。還有如果我們開發或者使用第三方的lib中的某些函數需要返回指標,這樣的 返回的指標被client使用的時候,lib就會失去對返回的指標的控制,這樣delete的指標的任務一般就會交給調用方client,但是如果 client忘記調用delete或是調用的時機不正確,都有可能導致問題,在這種情況下就最好使用智能指標。還有使用智能指標可以保證異常安全,保證程 序在有異常拋出時仍然無記憶體泄露。
   
    std::auto_ptr很多的時候並不能滿足我們的要求,比如她不能用在STL的container中。boost的smart_ptr中提供了4種智能指標和2種智能指標數組來作為std::auto_ptr的補充。   
      

    shared_ptr<boost/shared_ptr.hpp>:使用shared_ptr進行對象的生存期自動管理,使得分享資源所有權變得有效且安全.

    scoped_ptr<boost/scoped_ptr.hpp>: 用於確保能夠正確地刪除動態分配的對象。scoped_ptr 有著與std::auto_ptr類似的特性,而最大的區別在於它不能轉讓所有權而auto_ptr可以。事實上,scoped_ptr永遠不能被複製或 被賦值!scoped_ptr 擁有它所指向的資源的所有權,並永遠不會放棄這個所有權。

    weak_ptr<boost/weak_ptr.hpp>:weak_ptr 是 shared_ptr 的觀察員。它不會干擾shared_ptr所分享所有權。當一個被weak_ptr所觀察的 shared_ptr 要釋放它的資源時,它會把相關的 weak_ptr的指標設為空白。使用此輔助指標一般是防止懸null 指標。

    intrusive_ptr<boost/intrusive_ptr.hpp>:shared_ptr的插入是版本,一般不使用,因為需要對使用此指標的類型中增加ref計數功能。但是可以保證不增加指標的大小。

    scoped_array<boost/scoped_array.hpp>: scoped_array 為數組做了scoped_ptr為單個對象的指標所做的事情:它負責釋放記憶體。
    shared_array<boost/shared_array.hpp>: shared_array 用於共用數組所有權的智能指標。一般指向std::vector的shared_ptr提供了比shared_array更多的靈活性,所以一般使用 std::vector<shared_ptr>。

二 源碼剖析

    通過上面的分析,下面主要介紹我們最常用也最有用的shared_ptr智能指標。(智能指標的實現,其實最重要的是就是重載->和*運算子)

下面是shared_ptr的標頭檔:

template<class Ty> class shared_ptr {
public:
   typedef Ty element_type;

   shared_ptr();
   template<class Other>
    explicit shared_ptr(Other *ptr);
   template<class Other, class D>
     shared_ptr(Other *ptr, D dtor);
   shared_ptr(const shared_ptr& sp);
   template<class Other>
     shared_ptr(const shared_ptr<Other>& sp);
   template <class Other>
     shared_ptr(const weak_ptr<Other>& wp);
   template<class Other>
     shared_ptr(const std::auto_ptr<Other>& ap);
  ~shared_ptr();

   shared_ptr& operator=(const shared_ptr& sp);
   template<class Other>
     shared_ptr& operator=(const shared_ptr<Other>& sp);
   template<class Other>
     shared_ptr& operator=(auto_ptr<Other>& ap);

  void swap(shared_ptr& s);
  void reset();
   template<class Other>
    void reset(Other *ptr);
   template<class Other, class D>
    void reset(Other *ptr, D dtor);

   Ty *get() const;
   Ty& operator*() const;
   Ty *operator->() const;
  long use_count() const;
  bool unique() const;
  operator boolean-type() const;
   };

1)建構函式,可以通過一般指標,std::auto_ptr,boost::shared_ptr,boost::weak_ptr來構造,還可以構造的時候指定如果delete指標。
2)拷貝建構函式
3)get(), 得到boost::shared_ptr所封裝的指標。
4)*,得到boost::shared_ptr所封裝指標的值。
5)->,用於直接調用指標的成員。
6)reset(), 用於重設boost::shared_ptr,或設為空白。
7) swap(), 用於交換2個boost::shared_ptr.
8) use_count(), use_count 函數返回指標的引用計數。
9) unique(),這個函數在shared_ptr是它所儲存指標的唯一擁有者時返回 true ;否則返回 false。 unique 不會拋出異常。

 

三 執行個體

在STL容器中的使用:

#include "boost/shared_ptr.hpp"
#include <vector>
#include <iostream>
class A
{
public:  
    virtual void sing()
    {
         std::cout <<"A::sing" <<std::endl;
     }
protected:  
    virtual ~A()
    {std::cout<<"~A"<<std::endl;};
};
class B : public A
{
public:  
    virtual void sing()
    {   
         std::cout << "B:sing"<<std::endl;
     }
    virtual ~B()
    {std::cout<<"~B"<<std::endl;}
};
boost::shared_ptr<A> createA()
{
     boost::shared_ptr<A> p(new B());
    return p;
}
int main()
{  
     typedef std::vector<boost::shared_ptr<A> > container_type;
     typedef container_type::iterator iterator;
     container_type container;
    for (int i=0;i<10;++i)
    {   
         container.push_back(createA());
     }  
     std::cout << "The choir is gathered: /n";
     iterator end=container.end();
    for (iterator it=container.begin();it!=end;++it)
    {     (*it)->sing(); }
}

 

四 注意

 

五 參考

1)Beyond the C++ Standard Library: An Introduction to Boost
2)boost線上document

原文連結:http://www.cppblog.com/mzty/archive/2007/08/22/30611.html

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.