shared_ptr

转自:http://xiangjie88.iteye.com/blog/991257

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

C++代码 复制代码  收藏代码
  1. template<class T> class shared_ptr   
  2. {   
  3. public:   
  4.     typedef T element_type;   
  5.     //创建一个持有空指针的shared_ptr   
  6.     shared_ptr();   
  7.     //获得指向类型T的指针p的管理权,同时引用计数=1.这个构造函数要求Y类型必须能够转换成T类型   
  8.     template<class Y> explicit shared_ptr(Y *p);   
  9.     //和上面差不多,参数d指定了析构时的定制删除器   
  10.     template<class Y,class D> shared_ptr(Y* p,D d);   
  11.     ~shared_ptr();   
  12.   
  13.     //从另一个shared_ptr获得指针的管理权,同时引用计数+1,然后两个shared_ptr共享一个指针的管理权   
  14.     shared_ptr(shared_ptr const & r);   
  15.     //从一个auto_ptr获得指针的管理权,引用计数=1,同时auto_ptr自动失去管理权   
  16.     template<class Y> explicit shared_ptr(std::auto_ptr<Y>& r);   
  17.   
  18.     //从一个shared_ptr或auto_ptr获得管理权   
  19.     shared_ptr & operator=(shared_ptr const & r);   
  20.     template<class Y> shared_ptr & operator=(shared_ptr<Y> const & r);   
  21.     template<class Y> shared_ptr & operator=(std::auto_ptr<Y> & r);   
  22.   
  23.     //和scoped_ptr不完全一样,引用计数器-1,停止对指针的共享,改为管理另一个指针,如果引用计数<0,发生删除操作   
  24.     void reset();   
  25.     template<class Y> void reset(Y *p);   
  26.     template<class Y,class D> void reset(Y *p,D d);   
  27.   
  28.     T & operator*()const;   
  29.     T * operator->()const;   
  30.     T * get() const;   
  31.   
  32.     //在shared_ptr是指针的唯一所有者时返回true,比use_count()=1速度快,安全   
  33.     bool unique()const;   
  34.     //一般用于测试   
  35.     long use_count() const;   
  36.   
  37.     operator unspecified-bool-type()const;   
  38.     void swap(shared_ptr & b);   
  39. };  


不能使用常规的如static_cast之类的转型,要用就用static_pointer_cast<T>(),const_pointer_cast<T>(),dynamic_pointer_cast<T>()
C++代码 复制代码  收藏代码
  1. #include<boost/shared_ptr.hpp>   
  2. #include<iostream>   
  3. using namespace boost;   
  4. using namespace std;   
  5.   
  6. int main()   
  7. {   
  8.     shared_ptr<int> p(new int);   
  9.     *p = 10;   
  10.     cout << *p << endl;   
  11.     shared_ptr<int> p2 = static_pointer_cast<int>(p);   
  12.     cout << *p2 << endl;   
  13. }   
  14.   
  15. 10   
  16. 10  


C++代码 复制代码  收藏代码
  1. #include<boost/shared_ptr.hpp>   
  2. #include<boost/scoped_ptr.hpp>   
  3. #include<string>   
  4. #include<iostream>   
  5. using namespace boost;   
  6. using namespace std;   
  7.   
  8. void print(shared_ptr<string> p)   
  9. {   
  10.     cout << "count:" << p.use_count() << ",v=" << *p << endl;   
  11. }   
  12.   
  13. int main()   
  14. {   
  15.     shared_ptr<string> p(new string("abc"));   
  16.     cout << p.use_count() << endl;   
  17.     shared_ptr<string> p1(p);   
  18.     shared_ptr<string> p2(p);   
  19.     cout << p1.use_count() << "," << *p1 << endl;   
  20.     cout << p.use_count() << "," << *p << endl;   
  21.     cout << p2.use_count() << "," << *p2 << endl;   
  22.     print(p2);//内部函数拷贝了一个shared_ptr对象,引用计数+1   
  23.     cout << p2.use_count() << "," << *p2 << endl;//自动析构,引用计数恢复   
  24.     print(p1);   
  25.     print(p);   
  26. }   
  27.   
  28. 1   
  29. 3,abc   
  30. 3,abc   
  31. 3,abc   
  32. count:4,v=abc   
  33. 3,abc   
  34. count:4,v=abc   
  35. count:4,v=abc  


C++代码 复制代码  收藏代码
  1. #include<boost/shared_ptr.hpp>   
  2. #include<iostream>   
  3. #include<vector>   
  4. using namespace boost;   
  5. using namespace std;   
  6.   
  7. //桥接模式   
  8. class sample   
  9. {   
  10. private:   
  11.     class impl;   
  12.     shared_ptr<impl> p;   
  13. public:   
  14.     sample();   
  15.     void print();   
  16. };   
  17.   
  18. class sample::impl   
  19. {   
  20. public:   
  21.     void print(){cout<<"impl"<<endl;}   
  22. };   
  23.   
  24. sample::sample():p(new impl){}   
  25.   
  26. void sample::print(){   
  27.     p->print();   
  28. }   
  29.   
  30. int main()   
  31. {   
  32.     typedef vector< shared_ptr<int> > v;   
  33.     v v1(5);   
  34.     int i=0;   
  35.     for(v::iterator iter=v1.begin();iter!=v1.end();++iter){   
  36.         *iter = shared_ptr<int>(new int(++i));   
  37.         cout << *(*iter) << endl;   
  38.     }   
  39.     sample s;   
  40.     s.print();   
  41. }   
  42.   
  43. 1   
  44. 2   
  45. 3   
  46. 4   
  47. 5   
  48. impl  


C++代码 复制代码  收藏代码
  1. #include<boost/shared_ptr.hpp>   
  2. #include<iostream>   
  3. using namespace std;   
  4. using namespace boost;   
  5.   
  6. //工厂模式   
  7. class abstract   
  8. {   
  9. public:   
  10.     virtual void f()=0;   
  11.     virtual void g()=0;   
  12. protected:   
  13.     virtual ~abstract(){}   
  14. };   
  15.   
  16. class impl:public abstract   
  17. {   
  18. public:   
  19.     virtual void f()   
  20.     {   
  21.         cout << "class impl f" << endl;   
  22.     }   
  23.     virtual void g()   
  24.     {   
  25.         cout << "class impl g" << endl;   
  26.     }   
  27. };   
  28.   
  29. shared_ptr< abstract> create()   
  30. {   
  31.     return shared_ptr<abstract>(new impl);   
  32. }   
  33.   
  34. int main()   
  35. {   
  36.     shared_ptr<abstract> a = create();   
  37.     a->f();   
  38.     a->g();   
  39. }   
  40.   
  41. class impl f   
  42. class impl g  


shared_ptr能够存储void*型的指针,还可以定制删除器:
shared_ptr<void> p ((void*)0,delFun());

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值