转自:http://xiangjie88.iteye.com/blog/991257
shared_ptr早期叫做counted_ptr,它实现了引用计数型的智能指针,与scoped_ptr一样包装了new操作符在堆上分配的动态对象,但可以被自由地拷贝和赋值。同时它弥补了auto_ptr因为转移语义而不能把指针作为STL容器元素的缺陷。
不能使用常规的如static_cast之类的转型,要用就用static_pointer_cast<T>(),const_pointer_cast<T>(),dynamic_pointer_cast<T>()
shared_ptr能够存储void*型的指针,还可以定制删除器:
shared_ptr<void> p ((void*)0,delFun());
- 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);
- };
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<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<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> #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
#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());