C++:Boost库智能指针_shared_array
2016-08-13 22:00
375 查看
智能指针_shared_array: 共享智能指针(数组)
shared_array 类似 shared_ptr,它包装了new[]操作符在堆上分配的动态数组,同样使用引用计数机制为动态数组提供了一个代理,可以在程序的生命周期里长期存在,知道没有任何引用后才释放内存。
主要特点如下:
1.构造函数接收的指针p必须是new[]的结果,而不是new表达式的结果;
2.没有*、->操作符重载,因为scoped_array持有的不是一个普通指针;
3.析构函数使用delete[]释放资源,而不是delete;
4.提供operator[]操作符重载,可以像普通数组一样用下标访问元素;
使用:
<span style="font-size:18px;">#include <iostream>
#include <boost/smart_ptr.hpp>
using namespace std;
using namespace boost;
int main()
{
int *p = new int[10];
shared_array<int> pa(p);
for(int i = 0; i < 10; ++i)
{
pa[i] = i;
}
return 0;
}</span>
/////////////////////////////////////
实现:
<1>shared_count.h
<span style="font-size:18px;">#ifndef _SHARED_COUNT_H
#define _SHARED_COUNT_H
#include "sp_counted_base.h"
#include "sp_counted_impl_xx.h"
class shared_count
{
public:
template<class Y> //没有删除器
shared_count(Y *p):pi_(new sp_counted_impl_xx<Y>(p))
{}
template<class Y,class D> //具有删除器
shared_count(Y * p, D d): pi_(0)
{
typedef Y* p;
pi_ = new sp_counted_impl_pd<P,D>(p,d);
}
shared_count(const shared_count &r):pi_(r.pi_)
{
if(pi_ != 0)
{
pi_->add_ref_copy();
}
}
~shared_count()
{
if(pi_ != 0) //释放shared_count所指的空间
pi_->release();
}
public:
long use_count()const
{
return pi_ != 0 ? pi_->use_count : 0;
}
bool unique()const
{
return use_count() == 1;
}
void swap(shared_count &r)
{
sp_counted_base *tmp = r.pi;
r.pi_ = pi_;
pi_ = tmp;
}
private:
sp_counted_base *pi_; //父类指针作为接口,实现多态
};
#endif</span>
<1-2>sp_counted_base.h
<1-3>sp_counted_impl_xx.h
////////////////////////////////
<3>checked_array.h //定制数组删除器
<2>shared_array.h
////////////////////
main.cpp
shared_array 类似 shared_ptr,它包装了new[]操作符在堆上分配的动态数组,同样使用引用计数机制为动态数组提供了一个代理,可以在程序的生命周期里长期存在,知道没有任何引用后才释放内存。
主要特点如下:
1.构造函数接收的指针p必须是new[]的结果,而不是new表达式的结果;
2.没有*、->操作符重载,因为scoped_array持有的不是一个普通指针;
3.析构函数使用delete[]释放资源,而不是delete;
4.提供operator[]操作符重载,可以像普通数组一样用下标访问元素;
使用:
<span style="font-size:18px;">#include <iostream>
#include <boost/smart_ptr.hpp>
using namespace std;
using namespace boost;
int main()
{
int *p = new int[10];
shared_array<int> pa(p);
for(int i = 0; i < 10; ++i)
{
pa[i] = i;
}
return 0;
}</span>
/////////////////////////////////////
实现:
<1>shared_count.h
<span style="font-size:18px;">#ifndef _SHARED_COUNT_H
#define _SHARED_COUNT_H
#include "sp_counted_base.h"
#include "sp_counted_impl_xx.h"
class shared_count
{
public:
template<class Y> //没有删除器
shared_count(Y *p):pi_(new sp_counted_impl_xx<Y>(p))
{}
template<class Y,class D> //具有删除器
shared_count(Y * p, D d): pi_(0)
{
typedef Y* p;
pi_ = new sp_counted_impl_pd<P,D>(p,d);
}
shared_count(const shared_count &r):pi_(r.pi_)
{
if(pi_ != 0)
{
pi_->add_ref_copy();
}
}
~shared_count()
{
if(pi_ != 0) //释放shared_count所指的空间
pi_->release();
}
public:
long use_count()const
{
return pi_ != 0 ? pi_->use_count : 0;
}
bool unique()const
{
return use_count() == 1;
}
void swap(shared_count &r)
{
sp_counted_base *tmp = r.pi;
r.pi_ = pi_;
pi_ = tmp;
}
private:
sp_counted_base *pi_; //父类指针作为接口,实现多态
};
#endif</span>
<1-2>sp_counted_base.h
<span style="font-size:18px;">#ifndef _SP_COUNTED_BASE_H #define _SP_COUNTED_BASE_H class sp_counted_base { public: sp_counted_base():use_count_(1) {} virtual ~sp_counted_base() {} public: virtual void dispose()=0; void release() { if(--use_count_ == 0) { dispose(); //析构sp_counted_xx所指的数据空间 delete this; //析构自身sp_counted_imple_xx } } viod add_ref_copy() { ++use_count_; } public: long use_count()const { return use_count_; } private: long use_count_; }; #endif</span>
<1-3>sp_counted_impl_xx.h
<span style="font-size:18px;">#ifndef _SP_COUNTED_IMPL_XX_H #define _SP_COUNTED_IMPL_XX_H #include "sp_counted_base.h" template<class P, class D> //具有删除器的子类 class sp_counted_impl_pd:public sp_counted_base { public: sp_counted_impl_pd(P p, D d): ptr(p),del(d) {} ~sp_counted_impl_pd() {} public: virtual void dispose() { del(ptr); //调用删除器 } private: P ptr; D del; }; #endif</span>
////////////////////////////////
<3>checked_array.h //定制数组删除器
<span style="font-size:18px;">#ifndef _CHECKED_DELETE_H #define _CHECKED_DELETE_H void checked_array_delete(T * x) { delete []x; } template<class T> struct checked_array_deleter { void operator()(T * x)const //del.operator()(ptr) { checked_array_delete(x); } }; #endif</span>
<2>shared_array.h
<span style="font-size:18px;">#ifndef _SHARED_ARRAY_H #define _SHARED_ARRAY_H #include "shared_count.h" #include "checked_delete.h" template<class T> class shared_array { typedef checked_array_deleter<T> deleter; public: template<class Y,class D> //具有删除器的构造函数 shared_ptr(Y *p, D d):px(p),pn(p,deleter()) //使用函数对象作为删除器 {} ~shared_ptr() {} public: T& operator[](int i)const //重载[] { return px[i]; } private: T *px; shared_count pn; }; #endif</span>
////////////////////
main.cpp
<span style="font-size:18px;">int main() { int *p = new int[10]; shared_array<int> pa(p); for(int i = 0; i < 10; ++i) { pa[i] = i; } return 0; }</span>
相关文章推荐
- 看完了第二遍C++Primer,学习心得与问题
- C++:Boost库智能指针_shared_ptr
- VS2013中C++代码折叠
- C++中的覆盖和隐藏
- 1025. PAT Ranking (25)
- C++:Boost库智能指针_scoped_array
- C++:Boost库智能指针_scoped_ptr
- C++处理字符串的方法——字符串类与字符串变量
- C++中关于指针的理解
- C++中自增自减运算符疑惑大全
- C++:智能指针_auto_ptr_vs版本
- C++:智能指针_auto_ptr_vc版本
- C++常用的#include头文件总结
- 内存区划分、内存分配、常量存储区、堆、栈、自由存储区、全局区[C++][内存管理]
- C++:初识智能指针
- C/C++ sort函数的用法
- C++中 string 的用法大全
- C语言printf函数输出表达式中的计算顺序
- POJ 1416 - Shredding Compan
- 深入分析C++引用