引领Boost(四)(Boost::smart_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:使用shared_ptr进行对象的生存期自动管理,使得分享资源所有权变得有效且安全.

scoped_ptr: 用于确保能够正确地删除动态分配的对象。scoped_ptr 有着与std::auto_ptr类似的特性,而最大的区别在于它不能转让所有权而auto_ptr可以。事实上,scoped_ptr永远不能被复制或被赋值!scoped_ptr 拥有它所指向的资源的所有权,并永远不会放弃这个所有权。

weak_ptr:weak_ptr 是 shared_ptr 的观察员。它不会干扰shared_ptr所共享的所有权。当一个被weak_ptr所观察的 shared_ptr 要释放它的资源时,它会把相关的 weak_ptr的指针设为空。使用此辅助指针一般是防止悬空指针。

intrusive_ptr:shared_ptr的插入是版本,一般不使用,因为需要对使用此指针的类型中增加ref计数功能。但是可以保证不增加指针的大小。

scoped_array: scoped_array 为数组做了scoped_ptr为单个对象的指针所做的事情:它负责释放内存。

shared_array: shared_array 用于共享数组所有权的智能指针。一般指向std::vector的shared_ptr提供了比shared_array更多的灵活性,所以一般使用std::vector


二 源码剖析

通过上面的分析,下面主要介绍我们最常用也最有用的shared_ptr智能指针。(智能指针的实现,其实最重要的是就是重载->和*运算符)

下面是shared_ptr的头文件:
引领Boost(四)(Boost::smart_ptr)引领Boost(四)(Boost::smart_ptr)template<classTy>classshared_ptr{

引领Boost(四)(Boost::smart_ptr)
public:

引领Boost(四)(Boost::smart_ptr) typedef Ty element_type;

引领Boost(四)(Boost::smart_ptr)

引领Boost(四)(Boost::smart_ptr) shared_ptr();

引领Boost(四)(Boost::smart_ptr) template
<classOther>

引领Boost(四)(Boost::smart_ptr)
explicitshared_ptr(Otherptr);

引领Boost(四)(Boost::smart_ptr) template
<classOther,classD>

引领Boost(四)(Boost::smart_ptr) shared_ptr(Other
ptr, D dtor);

引领Boost(四)(Boost::smart_ptr) shared_ptr(
constshared_ptr&sp);

引领Boost(四)(Boost::smart_ptr) template
<classOther>

引领Boost(四)(Boost::smart_ptr) shared_ptr(
constshared_ptr<Other>&sp);

引领Boost(四)(Boost::smart_ptr) template
<classOther>

引领Boost(四)(Boost::smart_ptr) shared_ptr(
constweak_ptr<Other>&wp);

引领Boost(四)(Boost::smart_ptr) template
<classOther>

引领Boost(四)(Boost::smart_ptr) shared_ptr(
conststd::auto_ptr<Other>&ap);

引领Boost(四)(Boost::smart_ptr)
~shared_ptr();

引领Boost(四)(Boost::smart_ptr)

引领Boost(四)(Boost::smart_ptr) shared_ptr
&operator=(constshared_ptr&sp);

引领Boost(四)(Boost::smart_ptr) template
<classOther>

引领Boost(四)(Boost::smart_ptr) shared_ptr
&operator=(constshared_ptr<Other>&sp);

引领Boost(四)(Boost::smart_ptr) template
<classOther>

引领Boost(四)(Boost::smart_ptr) shared_ptr
&operator=(auto_ptr<Other>&ap);

引领Boost(四)(Boost::smart_ptr)

引领Boost(四)(Boost::smart_ptr)
voidswap(shared_ptr&s);

引领Boost(四)(Boost::smart_ptr)
voidreset();

引领Boost(四)(Boost::smart_ptr) template
<classOther>

引领Boost(四)(Boost::smart_ptr)
voidreset(Otherptr);

引领Boost(四)(Boost::smart_ptr) template
<classOther,classD>

引领Boost(四)(Boost::smart_ptr)
voidreset(Other
ptr, D dtor);

引领Boost(四)(Boost::smart_ptr)

引领Boost(四)(Boost::smart_ptr) Ty
get()const;

引领Boost(四)(Boost::smart_ptr) Ty
&operator
()const;

引领Boost(四)(Boost::smart_ptr) Ty
operator->()const;

引领Boost(四)(Boost::smart_ptr)
longuse_count()const;

引领Boost(四)(Boost::smart_ptr)
boolunique()const;

引领Boost(四)(Boost::smart_ptr)
operatorboolean-type()const;

引领Boost(四)(Boost::smart_ptr) }
;

引领Boost(四)(Boost::smart_ptr)

引领Boost(四)(Boost::smart_ptr)



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 不会抛出异常。

三 实例

1)构造,拷贝构造,赋值,get,,->, reset, swap:
引领Boost(四)(Boost::smart_ptr)#include"boost/shared_ptr.hpp"

引领Boost(四)(Boost::smart_ptr)#include
<cassert>

引领Boost(四)(Boost::smart_ptr)#include
<iostream>

引领Boost(四)(Boost::smart_ptr)

引领Boost(四)(Boost::smart_ptr)
classA

引领Boost(四)(Boost::smart_ptr)引领Boost(四)(Boost::smart_ptr)
{

引领Boost(四)(Boost::smart_ptr) boost::shared_ptr
<int>no_;

引领Boost(四)(Boost::smart_ptr)
public:

引领Boost(四)(Boost::smart_ptr)引领Boost(四)(Boost::smart_ptr) A(boost::shared_ptr
<int>no) : no_(no){}

引领Boost(四)(Boost::smart_ptr)引领Boost(四)(Boost::smart_ptr)
voidvalue(inti){
no_=i; }

引领Boost(四)(Boost::smart_ptr)}
;

引领Boost(四)(Boost::smart_ptr)
classB

引领Boost(四)(Boost::smart_ptr)引领Boost(四)(Boost::smart_ptr)
{

引领Boost(四)(Boost::smart_ptr) boost::shared_ptr
<int>no_;

引领Boost(四)(Boost::smart_ptr)
public:

引领Boost(四)(Boost::smart_ptr)引领Boost(四)(Boost::smart_ptr) B(boost::shared_ptr
<int>no) : no_(no){}

引领Boost(四)(Boost::smart_ptr)引领Boost(四)(Boost::smart_ptr)
intvalue()const{returnno_; }

引领Boost(四)(Boost::smart_ptr)}
;

引领Boost(四)(Boost::smart_ptr)
structdeleter

引领Boost(四)(Boost::smart_ptr)引领Boost(四)(Boost::smart_ptr)
{

引领Boost(四)(Boost::smart_ptr)
voidoperator()(int
i)

引领Boost(四)(Boost::smart_ptr)引领Boost(四)(Boost::smart_ptr)
{

引领Boost(四)(Boost::smart_ptr) std::cout
<<"destroying resource at"

引领Boost(四)(Boost::smart_ptr)
<<(void)i<<'\n';

引领Boost(四)(Boost::smart_ptr) delete i;

引领Boost(四)(Boost::smart_ptr) }


引领Boost(四)(Boost::smart_ptr)}
;

引领Boost(四)(Boost::smart_ptr)
structS

引领Boost(四)(Boost::smart_ptr)引领Boost(四)(Boost::smart_ptr)
{

引领Boost(四)(Boost::smart_ptr)
intmember;

引领Boost(四)(Boost::smart_ptr)}
;

引领Boost(四)(Boost::smart_ptr)

引领Boost(四)(Boost::smart_ptr)
intmain()

引领Boost(四)(Boost::smart_ptr)引领Boost(四)(Boost::smart_ptr)
{

引领Boost(四)(Boost::smart_ptr)
//test for constructor

引领Boost(四)(Boost::smart_ptr)
boost::shared_ptr<int>sp;

引领Boost(四)(Boost::smart_ptr) boost::shared_ptr
<int>sp2((int
)0);

引领Boost(四)(Boost::smart_ptr)引领Boost(四)(Boost::smart_ptr)
{

引领Boost(四)(Boost::smart_ptr) boost::shared_ptr
<int>sp3(newint(3), deleter());

引领Boost(四)(Boost::smart_ptr) }


引领Boost(四)(Boost::smart_ptr)

引领Boost(四)(Boost::smart_ptr) std::auto_ptr
<int>temp(newint(10));

引领Boost(四)(Boost::smart_ptr) boost::shared_ptr
<int>sp4(temp);

引领Boost(四)(Boost::smart_ptr)

引领Boost(四)(Boost::smart_ptr) boost::shared_ptr
<int>temp2(newint(14));

引领Boost(四)(Boost::smart_ptr) boost::shared_ptr
<int>sp5(temp2);

引领Boost(四)(Boost::smart_ptr)

引领Boost(四)(Boost::smart_ptr)

引领Boost(四)(Boost::smart_ptr)
//test for using the shared_ptr(get,->,)

引领Boost(四)(Boost::smart_ptr)
int
ip=newint(3);

引领Boost(四)(Boost::smart_ptr) std::cout
<<(void)ip<<'\n';

引领Boost(四)(Boost::smart_ptr) boost::shared_ptr
<int>sp6(ip);

引领Boost(四)(Boost::smart_ptr) std::cout
<<(void
)sp6.get()<<'\n';

引领Boost(四)(Boost::smart_ptr)

引领Boost(四)(Boost::smart_ptr)
intip2=newint(3);

引领Boost(四)(Boost::smart_ptr) std::cout
<<(void
)ip2<<'\n';

引领Boost(四)(Boost::smart_ptr) boost::shared_ptr
<int>sp7(ip2);

引领Boost(四)(Boost::smart_ptr) std::cout
<<sp7<<'\n';

引领Boost(四)(Boost::smart_ptr) std::cout
<<(void
)&sp7<<'\n';

引领Boost(四)(Boost::smart_ptr)

引领Boost(四)(Boost::smart_ptr) S
s=newS;

引领Boost(四)(Boost::smart_ptr) s
->member=4;

引领Boost(四)(Boost::smart_ptr) boost::shared_ptr
<S>sp8(s);

引领Boost(四)(Boost::smart_ptr) std::cout
<<sp8->member<<'\n';

引领Boost(四)(Boost::smart_ptr)

引领Boost(四)(Boost::smart_ptr)
//test for assign

引领Boost(四)(Boost::smart_ptr)
std::cout<<std::boolalpha;

引领Boost(四)(Boost::smart_ptr) boost::shared_ptr
<int>sp9(newint(0));

引领Boost(四)(Boost::smart_ptr) boost::shared_ptr
<int>sp10=sp9;

引领Boost(四)(Boost::smart_ptr) std::cout
<<"sp0 == sp1:"<<(sp9==sp10)<<'\n';

引领Boost(四)(Boost::smart_ptr) std::cout
<<"sp0 != sp2:"<<(sp9!=sp10)<<'\n';

引领Boost(四)(Boost::smart_ptr)

引领Boost(四)(Boost::smart_ptr)
//test funcion: reset and swap

引领Boost(四)(Boost::smart_ptr)
boost::shared_ptr<int>sp11 (newint(0));

引领Boost(四)(Boost::smart_ptr) boost::shared_ptr
<int>sp12 (newint(1));

引领Boost(四)(Boost::smart_ptr) sp11. swap (sp12);

引领Boost(四)(Boost::smart_ptr) std::cout
<<sp11<<sp12<<std::endl;

引领Boost(四)(Boost::smart_ptr) boost::swap(sp11, sp12);

引领Boost(四)(Boost::smart_ptr) std::cout
<<sp11<<sp12<<std::endl;

引领Boost(四)(Boost::smart_ptr)

引领Boost(四)(Boost::smart_ptr) boost::shared_ptr
<int>sp0;

引领Boost(四)(Boost::smart_ptr) sp0.reset();

引领Boost(四)(Boost::smart_ptr) boost::shared_ptr
<int>sp01(newint(1));

引领Boost(四)(Boost::smart_ptr) std::cout
<<sp01<<std::endl;

引领Boost(四)(Boost::smart_ptr) sp01.reset();

引领Boost(四)(Boost::smart_ptr)
//std::cout <<
sp01 <<std::endl;//error

引领Boost(四)(Boost::smart_ptr)
sp01.reset(newint(100));

引领Boost(四)(Boost::smart_ptr) std::cout
<<sp01<<std::endl;

引领Boost(四)(Boost::smart_ptr)

引领Boost(四)(Boost::smart_ptr)

引领Boost(四)(Boost::smart_ptr)

引领Boost(四)(Boost::smart_ptr)
//test for query shared_ptr's state (use_count,unique)

引领Boost(四)(Boost::smart_ptr)
typedef boost::shared_ptr<int>spi;

引领Boost(四)(Boost::smart_ptr) spi sp13 ;

引领Boost(四)(Boost::smart_ptr) std::cout
<<"empty object:"<<sp13.use_count()<<'\n';

引领Boost(四)(Boost::smart_ptr) spi sp14 ((
int
)0);

引领Boost(四)(Boost::smart_ptr) std::cout
<<"null pointer:"<<sp14.use_count()<<'\n';

引领Boost(四)(Boost::smart_ptr) spi sp15 (
newint);

引领Boost(四)(Boost::smart_ptr) std::cout
<<"one object:"<<sp15.use_count()<<'\n';

引领Boost(四)(Boost::smart_ptr)引领Boost(四)(Boost::smart_ptr)
{

引领Boost(四)(Boost::smart_ptr) spi sp16(sp15);

引领Boost(四)(Boost::smart_ptr) std::cout
<<"two objects:"<<sp15.use_count()<<'\n';

引领Boost(四)(Boost::smart_ptr) std::cout
<<"two objects:"<<sp16.use_count()<<'\n';

引领Boost(四)(Boost::smart_ptr) }


引领Boost(四)(Boost::smart_ptr) std::cout
<<"one object:"<<sp15.use_count()<<'\n';

引领Boost(四)(Boost::smart_ptr)

引领Boost(四)(Boost::smart_ptr) std::cout
<<std::boolalpha;

引领Boost(四)(Boost::smart_ptr) spi sp17;

引领Boost(四)(Boost::smart_ptr) std::cout
<<"empty object:"<<sp17.unique()<<'\n';

引领Boost(四)(Boost::smart_ptr) spi sp18((
int*)0);

引领Boost(四)(Boost::smart_ptr) std::cout
<<"null pointer:"<<sp18.unique()<<'\n';

引领Boost(四)(Boost::smart_ptr) spi sp19(
newint);

引领Boost(四)(Boost::smart_ptr) std::cout
<<"one object:"<<sp19.unique()<<'\n';

引领Boost(四)(Boost::smart_ptr)引领Boost(四)(Boost::smart_ptr)
{

引领Boost(四)(Boost::smart_ptr) spi sp20(sp19);

引领Boost(四)(Boost::smart_ptr) std::cout
<<"two objects:"<<sp19.unique()<<'\n';

引领Boost(四)(Boost::smart_ptr) std::cout
<<"two objects:"<<sp20.unique()<<'\n';

引领Boost(四)(Boost::smart_ptr) }


引领Boost(四)(Boost::smart_ptr) std::cout
<<"one object:"<<sp19.unique()<<'\n';

引领Boost(四)(Boost::smart_ptr)

引领Boost(四)(Boost::smart_ptr)}

2) 在STL容器中的使用:
引领Boost(四)(Boost::smart_ptr)#include"boost/shared_ptr.hpp"

引领Boost(四)(Boost::smart_ptr)#include
<vector>

引领Boost(四)(Boost::smart_ptr)#include
<iostream>

引领Boost(四)(Boost::smart_ptr)
classA

引领Boost(四)(Boost::smart_ptr)引领Boost(四)(Boost::smart_ptr)
{

引领Boost(四)(Boost::smart_ptr)
public:

引领Boost(四)(Boost::smart_ptr)
virtualvoidsing()

引领Boost(四)(Boost::smart_ptr)引领Boost(四)(Boost::smart_ptr)
{

引领Boost(四)(Boost::smart_ptr) std::cout
<<"A::sing"<<std::endl;

引领Boost(四)(Boost::smart_ptr) }


引领Boost(四)(Boost::smart_ptr)
protected:

引领Boost(四)(Boost::smart_ptr)
virtual~A()

引领Boost(四)(Boost::smart_ptr)引领Boost(四)(Boost::smart_ptr)
{std::cout<<"~A"<<std::endl;};

引领Boost(四)(Boost::smart_ptr)}
;

引领Boost(四)(Boost::smart_ptr)
classB :publicA

引领Boost(四)(Boost::smart_ptr)引领Boost(四)(Boost::smart_ptr)
{

引领Boost(四)(Boost::smart_ptr)
public:

引领Boost(四)(Boost::smart_ptr)
virtualvoidsing()

引领Boost(四)(Boost::smart_ptr)引领Boost(四)(Boost::smart_ptr)
{

引领Boost(四)(Boost::smart_ptr) std::cout
<<"B:sing"<<std::endl;

引领Boost(四)(Boost::smart_ptr) }


引领Boost(四)(Boost::smart_ptr)
virtual~B()

引领Boost(四)(Boost::smart_ptr)引领Boost(四)(Boost::smart_ptr)
{std::cout<<"~B"<<std::endl;}

引领Boost(四)(Boost::smart_ptr)}
;

引领Boost(四)(Boost::smart_ptr)boost::shared_ptr
<A>createA()

引领Boost(四)(Boost::smart_ptr)引领Boost(四)(Boost::smart_ptr)
{

引领Boost(四)(Boost::smart_ptr) boost::shared_ptr
<A>p(newB());

引领Boost(四)(Boost::smart_ptr)
returnp;

引领Boost(四)(Boost::smart_ptr)}


引领Boost(四)(Boost::smart_ptr)
intmain()

引领Boost(四)(Boost::smart_ptr)引领Boost(四)(Boost::smart_ptr)
{

引领Boost(四)(Boost::smart_ptr) typedef std::vector
<boost::shared_ptr<A>>container_type;

引领Boost(四)(Boost::smart_ptr) typedef container_type::iterator iterator;

引领Boost(四)(Boost::smart_ptr) container_type container;

引领Boost(四)(Boost::smart_ptr)
for(inti=0;i<10;++i)

引领Boost(四)(Boost::smart_ptr)引领Boost(四)(Boost::smart_ptr)
{

引领Boost(四)(Boost::smart_ptr) container.push_back(createA());

引领Boost(四)(Boost::smart_ptr) }


引领Boost(四)(Boost::smart_ptr) std::cout
<<"The choir is gathered: \n";

引领Boost(四)(Boost::smart_ptr) iterator end
=container.end();

引领Boost(四)(Boost::smart_ptr)
for(iterator it=container.begin();it!=end;++it)

引领Boost(四)(Boost::smart_ptr)引领Boost(四)(Boost::smart_ptr)
{ (*it)->sing(); }

引领Boost(四)(Boost::smart_ptr)}

四 注意

五 参考

1)Beyond the C++ Standard Library: An Introduction to Boost

2)boost在线document
原文链接: https://www.cnblogs.com/lzjsky/archive/2011/01/13/1934720.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/20029

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月7日 下午9:26
下一篇 2023年2月7日 下午9:26

相关推荐