shared_ptr

shared_ptr早期叫做counted_ptr,它实现了引用计数型的智能指针,与scoped_ptr一样包装了new操作符在堆上分配的动态对象,但可以被自由地拷贝和赋值。同时它弥补了auto_ptr因为转移语义而不能把指针作为STL容器元素的缺陷。

share_ptr是强引用,像铁丝绑住堆上的对象,只要有一个指向x对象的shared_ptr存在,该x对象就不会析构,它是原子操作,没有用锁


template<class T> class shared_ptr
{
public:
typedef T element_type;
//创建一个持有空指针的shared_ptr
shared_ptr();
//获得指向类型T的指针p的管理权,同时引用计数=1.这个构造函数要求Y类型必须能够转换成T类型
template<class Y> explicit shared_ptr(Y *p);
//和上面差不多,参数d指定了析构时的定制删除器
template<class Y,class D> shared_ptr(Y* p,D d);
~shared_ptr();

//从另一个shared_ptr获得指针的管理权,同时引用计数+1,然后两个shared_ptr共享一个指针的管理权
shared_ptr(shared_ptr const & r);
//从一个auto_ptr获得指针的管理权,引用计数=1,同时auto_ptr自动失去管理权
template<class Y> explicit shared_ptr(std::auto_ptr<Y>& r);

//从一个shared_ptr或auto_ptr获得管理权
shared_ptr & operator=(shared_ptr const & r);
template<class Y> shared_ptr & operator=(shared_ptr<Y> const & r);
template<class Y> shared_ptr & operator=(std::auto_ptr<Y> & r);

//和scoped_ptr不完全一样,引用计数器-1,停止对指针的共享,改为管理另一个指针,如果引用计数<0,发生删除操作
void reset();
template<class Y> void reset(Y *p);
template<class Y,class D> void reset(Y *p,D d);

T & operator*()const;
T * operator->()const;
T * get() const;

//在shared_ptr是指针的唯一所有者时返回true,比use_count()=1速度快,安全
bool unique()const;
//一般用于测试
long use_count() const;

operator unspecified-bool-type()const;
void swap(shared_ptr & b);
};



不能使用常规的如static_cast之类的转型,要用就用static_pointer_cast<T>(),const_pointer_cast<T>(),dynamic_pointer_cast<T>()

#include<boost/shared_ptr.hpp>
#include<iostream>
using namespace boost;
using namespace std;

int main()
{
shared_ptr<int> p(new int);
*p = 10;
cout << *p << endl;
shared_ptr<int> p2 = static_pointer_cast<int>(p);
cout << *p2 << endl;
}

10
10



#include<boost/shared_ptr.hpp>
#include<boost/scoped_ptr.hpp>
#include<string>
#include<iostream>
using namespace boost;
using namespace std;

void print(shared_ptr<string> p)
{
cout << "count:" << p.use_count() << ",v=" << *p << endl;
}

int main()
{
shared_ptr<string> p(new string("abc"));
cout << p.use_count() << endl;
shared_ptr<string> p1(p);
shared_ptr<string> p2(p);
cout << p1.use_count() << "," << *p1 << endl;
cout << p.use_count() << "," << *p << endl;
cout << p2.use_count() << "," << *p2 << endl;
print(p2);//内部函数拷贝了一个shared_ptr对象,引用计数+1
cout << p2.use_count() << "," << *p2 << endl;//自动析构,引用计数恢复
print(p1);
print(p);
}

1
3,abc
3,abc
3,abc
count:4,v=abc
3,abc
count:4,v=abc
count:4,v=abc



#include<boost/shared_ptr.hpp>
#include<iostream>
#include<vector>
using namespace boost;
using namespace std;

//桥接模式
class sample
{
private:
class impl;
shared_ptr<impl> p;
public:
sample();
void print();
};

class sample::impl
{
public:
void print(){cout<<"impl"<<endl;}
};

sample::sample():p(new impl){}

void sample::print(){
p->print();
}

int main()
{
typedef vector< shared_ptr<int> > v;
v v1(5);
int i=0;
for(v::iterator iter=v1.begin();iter!=v1.end();++iter){
*iter = shared_ptr<int>(new int(++i));
cout << *(*iter) << endl;
}
sample s;
s.print();
}

1
2
3
4
5
impl



#include<boost/shared_ptr.hpp>
#include<iostream>
using namespace std;
using namespace boost;

//工厂模式
class abstract
{
public:
virtual void f()=0;
virtual void g()=0;
protected:
virtual ~abstract(){}
};

class impl:public abstract
{
public:
virtual void f()
{
cout << "class impl f" << endl;
}
virtual void g()
{
cout << "class impl g" << endl;
}
};

shared_ptr< abstract> create()
{
return shared_ptr<abstract>(new impl);
}

int main()
{
shared_ptr<abstract> a = create();
a->f();
a->g();
}

class impl f
class impl g


shared_ptr能够存储void*型的指针,还可以定制删除器:
shared_ptr<void> p ((void*)0,delFun());
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值