您的位置:首页 > 运维架构

Boost::smart_Ptr makes up for AutoPtr

2011-02-23 16:54 295 查看
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的指针设为空。使用此辅助指针一般是防止悬空指针。

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: