Boost库中shared_ptr(上)
1、共享性智能指针(shared_ptr)
引用计数型指针
shared_ptr是一个最像指针的“智能指针”,是boost.smart_ptr库中最有价值,最重要,也是最有用的。
shared_ptr实现的是引用技术型的智能指针,可以被拷贝和赋值,在任意地方共享它,当没有代码使用(此时引用计数为0)它才删除被动态分配的对象。shared_ptr也可以被安全的放到标准容器中;
2、怎么使用shared_ptr
举一个操作的例子:
#include<iostream>#include<boost/smart_ptr.hpp>usingnamespacestd;usingnamespaceboost;intmain(void){int*p=newint(10);shared_ptr<int>ps(p);//cout<<*ps<<endl;cout<<ps.unique()<<endl;//判断对空间是否唯一,cout<<ps.use_count()<<endl;shared_ptr<int>ps1=ps;cout<<ps.unique()<<endl;//此时有两个对空间有共享,所以不唯一,是0cout<<ps.use_count()<<endl;shared_ptr<int>ps2;ps2=ps1;cout<<ps.use_count()<<endl;}
关键在shared_ptr中存在共享引用计数。
3、框架的搭建
阅读源代码:
shared_ptr 中的私有数据成员:
private:T*px;shared_countpn;//对象成员,肯定先调这个对象的构造函数;
之前的引用计数通过一个指针,现在的引用计数通过一个对象,pn
构造函数的调用顺序:先虚基类,父类,对象成员,最后构造自己;
此时的模型如下:
其后调用对象成员的构造函数:
shared_counted中的私有数据成员:
private:sp_counted_base*pi;//有一个指向引用计数器父类的指针;
此时就得先写:sp_counted_base类了;
sp_counted_base类中的私有数据成员:
private:longuse_count_;
然后看到在shared_counted的构造函数:
public:template<classT>//此时类型不定,写模板函数shared_count(T*p):pi(newsp_counted_impl_xx(p)){//特别重要,这个构造函数
此时就得写sp_counted_impl_xx类了:这是继承sp_counted_base类
其内部数据时成员:
private:T*px_;
此时整体的建构体系就已经形成:
我认为是这样的:
(1)、先实现了shared_ptr类,因为有对象成员,其后调用构造函数,
(2)、实现了shared_count; 其数据成员有sp_counted_base,
(3)、因为编译器的顺序,先类名,在数据成员,最后函数,所以此时先实现sp_counted_base;
(4)、因为shared_counted中的构造函数要在堆上开辟sp_counted_impl_xx空间,最后实现是sp_counted_impl_xx,它有继承sp_counted_base,所以构造函数的调用顺序就很清楚了。
构造函数的调用顺序:sp_counted_base、sp_counted_impl_xx、shared_count、shared_ptr
此时的具体实现代码如下:
#ifndef_CONFIG_H_#define_CONFIG_H_#include<iostream>usingnamespacestd;#endif////////////////////////////////////////////////////////////////////////////#ifndef_SHARED_PTR_H_#define_SHARED_PTR_H_#include"shared_count.h"template<classT>classshared_ptr{public:shared_ptr(T*p=0):px(p),pn(p){cout<<"Createshared_ptrobject!"<<endl;}~shared_ptr(){cout<<"Freeshared_ptrobject"<<endl;}private:T*px;shared_countpn;};#endif///////////////////////////////////////////////////////////////////////////////#ifndef_SHARED_COUNT_H_#define_SHARED_COUNT_H_#include"config.h"#include"sp_counted_base.h"#include"sp_counted_impl_xx.h"classshared_count{public:template<classT>//此时类型不定,写模板函数shared_count(T*p):pi(newsp_counted_impl_xx<T>(p)){cout<<"Createshared_coutobject!"<<endl;}~shared_count(){cout<<"Freeshared_countobject"<<endl;}private:sp_counted_base*pi;};#endif///////////////////////////////////////////////////////////////////////////////#ifndefSP_COUNTED_BASE_H_#defineSP_COUNTED_BASE_H_#include"config.h"classsp_counted_base{public:sp_counted_base():use_count_(1){cout<<"Createsp_counted_baseobject"<<endl;}~sp_counted_base(){cout<<"Freesp_counted_baseobject"<<endl;}private:longuse_count_;};#endif//////////////////////////////////////////////////////////////////////////////////////#ifndefSP_COUNTED_IMPL_XX_H_#defineSP_COUNTED_IMPL_XX_H_#include"sp_counted_base.h"template<classT>classsp_counted_impl_xx:publicsp_counted_base{public:sp_counted_impl_xx(T*p):px_(p){cout<<"Createsp_counted_impl_xxobject"<<endl;}~sp_counted_impl_xx(){cout<<"Freesp_counted_impl_xxobject"<<endl;}private:T*px_;};#endif//////////////////////////////////////////////////////////////////////////////////////////////#include<iostream>#include"shared_ptr.h"usingnamespacestd;intmain(void){int*p=newint(10);shared_ptr<int>ps(p);}
以下是运行结果:
以上就是只搭好了大致的框架,并没有考虑内存泄漏,析构的具体写法和其它函数的实现;
那么整个模型如下:
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。