(轉)引領Boost(四)Boost::smart_ptr
作者:夢在天涯 來源:C++博客 酷勤網收集 2007-09-12
std::auto_ptr很多的時候並不能滿足我們的要求,比如她不能用在STL的container中。boost的smart_ptr中提供了4種智能指針和2種智能指針數組來作爲std::auto_ptr的補充。
一 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進行對象的生存期自動管理,使得分享資源所有權變得有效且安全. (redwolf注:基於計數器的智能指針。同時,它只能作爲單個指針使用,不能指向數組,也不能構造成數組式的指針。)
scoped_ptr<boost/scoped_ptr.hpp>: 用於確保能夠正確地刪除動態分配的對象。scoped_ptr 有着與std::auto_ptr類似的特性,而最大的區別在於它不能轉讓所有權而auto_ptr可以。事實上,scoped_ptr永遠不能被複制或被賦值!scoped_ptr 擁有它所指向的資源的所有權,並永遠不會放棄這個所有權。(redwolf注:在所有權這一點上,類似於c中的void* const ptr;一類聲明。不能被複制和賦值是因爲它沒有提供public形式的複製構造函數和重載=運算符。同shared_ptr一樣,它也只能用於單個指針。)
weak_ptr<boost/weak_ptr.hpp>:weak_ptr 是 shared_ptr 的觀察員。它不會干擾shared_ptr所共享的所有權。當一個被weak_ptr所觀察的 shared_ptr 要釋放它的資源時,它會把相關的 weak_ptr的指針設爲空。使用此輔助指針一般是防止懸空指針。(懸空指針也稱野指針,指的是指向當時不合法的變量地址的指針。比如:未初始化的指針,使用過後釋放內存後但沒有置空的指針等。這樣的指針如果直接或者間接引用的話,一般導致程序的致命錯誤。)
intrusive_ptr<boost/intrusive_ptr.hpp>:shared_ptr的插入式版本,一般不使用,因爲需要對使用此指針的類型中增加ref計數功能。但是可以保證不增加指針的大小。
scoped_array<boost/scoped_array.hpp>: scoped_array 爲數組做了scoped_ptr爲單個對象的指針所做的事情:它負責釋放內存。(redwolf注:類似於const類型的vector。)
shared_array<boost/shared_array.hpp>: shared_array 用於共享數組所有權的智能指針。一般指向std::vector的shared_ptr提供了比shared_array更多的靈活性,所以一般使用std::vector<shared_ptr>。(redwolf注:用vector替換行嗎?)
通過上面的分析,下面主要介紹我們最常用也最有用的shared_ptr智能指針。(智能指針的實現,其實最重要的是就是重載->和*運算符)
下面是shared_ptr的頭文件:(redwolf注:爲什麼有的函數有兩個版本,一個是模板參數的一個是非模板參數的?)
template<class Ty> class shared_ptr {
public:
typedef Ty element_type;
shared_ptr();
template<class Other>
explicit shared_ptr(Other *ptr); (redwolf注:強制顯示構造)
template<class Other, class D>
shared_ptr(Other *ptr, D dtor); (redwolf注:由普通指針構造)
shared_ptr(const shared_ptr& sp); (redwolf注:可以複製)
template<class Other>
shared_ptr(const shared_ptr<Other>& sp);
template <class Other>
shared_ptr(const weak_ptr<Other>& wp); (redwolf注:由weak_ptr構造,相當於 類型轉換)
template<class Other>
shared_ptr(const std::auto_ptr<Other>& ap); (redwolf注:類型轉換)
~shared_ptr();
shared_ptr& operator=(const shared_ptr& sp); (redwolf注:可以賦值)
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指針(redwolf注:用戶自己提供析構器,智能指針在刪除時不調用默認的析構器(deleter)。析構器並不是析構函數。)。
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 不會拋出異常。
1)構造,拷貝構造,賦值,get,*,->, reset, swap:
#include "boost/shared_ptr.hpp"
#include <cassert>
#include <iostream>
class A
{
boost::shared_ptr<int> no_;
public:
A(boost::shared_ptr<int> no) : no_(no) {}
void value(int i) { *no_=i; }
};
class B
{
boost::shared_ptr<int> no_;
public:
B(boost::shared_ptr<int> no) : no_(no) {}
int value() const { return *no_; }
};
struct deleter
{
void operator()(int *i)
{
std::cout << "destroying resource at"
<< (void*)i << '\n';
delete i;
}
};
struct S
{
int member;
};
int main()
{
// test for constructor
boost::shared_ptr<int> sp;
boost::shared_ptr<int> sp2((int*)0);
{
boost::shared_ptr<int> sp3(new int(3), deleter());
}
std::auto_ptr<int> temp(new int(10));
boost::shared_ptr<int> sp4(temp);
boost::shared_ptr<int> temp2(new int(14));
boost::shared_ptr<int> sp5(temp2);
// test for using the shared_ptr(get,->,*)
int *ip = new int(3);
std::cout << (void*)ip << '\n';
boost::shared_ptr<int> sp6(ip);
std::cout << (void*)sp6.get () << '\n';
int *ip2 = new int(3);
std::cout << (void*)ip2 << '\n';
boost::shared_ptr<int> sp7(ip2);
std::cout << *sp7 << '\n';
std::cout << (void*)&*sp7 << '\n';
S *s = new S;
s->member = 4;
boost::shared_ptr<S> sp8(s);
std::cout << sp8 -> member << '\n';
// test for assign
std::cout << std::boolalpha;
boost::shared_ptr<int> sp9(new int(0));
boost::shared_ptr<int> sp10 = sp9;
std::cout << "sp0 == sp1:" << (sp9 == sp10) << '\n';
std::cout << "sp0 != sp2:" << (sp9 != sp10) << '\n';
// test funcion: reset and swap
boost::shared_ptr<int> sp11 (new int(0));
boost::shared_ptr<int> sp12 (new int(1));
sp11. swap (sp12);
std::cout<<*sp11<<*sp12<<std::endl;
boost::swap(sp11, sp12);
std::cout<<*sp11<<*sp12<<std::endl;
boost::shared_ptr<int> sp0;
sp0.reset();
boost::shared_ptr<int> sp01(new int(1));
std::cout << *sp01 <<std::endl;
sp01.reset();
//std::cout << *sp01 <<std::endl; //error
sp01.reset(new int(100));
std::cout << *sp01 <<std::endl;
// test for query shared_ptr's state (use_count,unique)
typedef boost::shared_ptr<int> spi;
spi sp13 ;
std::cout << "empty object: " << sp13.use_count() << '\n';
spi sp14 ((int *)0);
std::cout << "null pointer: " << sp14.use_count() << '\n';
spi sp15 (new int);
std::cout << "one object: " << sp15.use_count() << '\n';
{
spi sp16(sp15);
std::cout << "two objects: " << sp15.use_count() << '\n';
std::cout << "two objects: " << sp16.use_count() << '\n';
}
std::cout << "one object: " << sp15.use_count() << '\n';
std::cout << std::boolalpha;
spi sp17;
std::cout << "empty object: " << sp17.unique() << '\n';
spi sp18((int *)0);
std::cout << "null pointer: " << sp18.unique() << '\n';
spi sp19(new int);
std::cout << "one object: " << sp19.unique() << '\n';
{
spi sp20(sp19);
std::cout << "two objects: " << sp19.unique() << '\n';
std::cout << "two objects: " << sp20.unique() << '\n';
}
std::cout << "one object: " << sp19.unique() << '\n';
}
#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(); }
}