boost smart_prt 4-> shared_ptr
2017-02-26 12:00
323 查看
shared_ptr P84
引用计数型的智能指针shared_ptr<int> spi(new int); assert(spi); *spi = 253; shared_ptr<string> sps(new string("smart")); assert(sps->size() == 5);
shared_ptr提供operator<比较操作符,可用于标准关联容器
#include <iostream> #include <string> #include <map> #include <boost/assert.hpp> #include <boost/smart_ptr.hpp> using namespace std; using namespace boost; int main() { typedef boost::shared_ptr<string> sp_t; //shared_ptr类型定义 map<sp_t,int> m; //标准映射容器 sp_t sp(new string("one")); //一个shared_ptr对象 sp_t spb(new string("two")); m[sp] = 111; //关联数组用法 m[spb] = 222; return 0; }
编写基于虚函数的多态代码时指针类型转换 P87
static_pointer_cast()、const_pointer_cast()、dynamic_pointer_cast(),返回类型为shared_ptr
支持流输出操作符operator<<,输出内部指针值,方便调试。
示例用法
shared_ptr<int> sp(new int(10)); //一个指向整数的shared_ptr assert(sp.unique()); //现在shared_ptr是指针的惟一持有者 shared_ptr<int> sp2 = sp; //第二个shared_ptr,拷贝构造函数 //两个shared_ptr相等,指向同一个对象,引用计数为2 assert(sp == sp2 && sp.use_count() == 2); *sp2 = 100; //使用解引用操作符修改被指对象 assert(*sp == 100); //另一个shared_ptr也同时被修改 sp.reset(); //停止sp shared_ptr的使用 assert(!sp); //sp不再持有任何指针(空指针) assert(sp2.unique());
class shared //一个拥有shared_ptr的类 { private: shared_ptr<int> p; public: shared(shared_ptr<int> p_):p(p_) {} void print() { cout << "count:" << p.use_count() << " v = " <<*p <<endl; } }; void print_func(shared_ptr<int> p) //使用shared_ptr作为函数参数 { cout << "count:" << p.use_count() << " v = " <<*p <<endl; } int main() { shared_ptr<int> p(new int(100)); shared s1(p), s2(p); //构造两个自定义类 s1.print(); s2.print(); *p = 20; print_func(p); s1.print(); return 0; }
工厂函数
显示消除new
make_pair(),该函数要比直接创建shared_ptr对象的方式快且高效,内部仅分配一次内存,消除了shared_ptr构造时的开销。
用法
#include <boost/make_shared.hpp> int main() { shared_ptr<string> sp = make_shared<string>("make_shared"); shared_ptr<vector<int> > spv = make_shared<vector<int> >(10,2); assert(spv->size() == 10); }
应用于标准容器
将容器作为shared_ptr管理的对象,如
shared_ptr<list<T> >,使容器可以被安全共享,用法与普通shared_ptr相同。
将shared_ptr作为容器的元素
示例
#include <boost/make_shared.hpp> int main() { typedef vector<shared_ptr<int> > vs; vs v(10); //声明一个拥有10个元素的容器,元素被初始化为空指针 int i = 0; for(vs::iterator pso = v.begin(); pos != v.end(); ++pos) { (*pos) = make_shared<int>(++i); //使用工厂函数赋值 cout<<*(*pos)<<", "; //输出值 } cout <<endl; shared_ptr<int> p = v[9]; *p = 100; cout<<*v[9] <<endl; }
应用于桥接模式 P91
应用于工厂模式 P92
定制删除器 P93
相关文章推荐
- boost smart_ptr -> scoped_ptr
- 回调函数 typedef bool (*IsUsed)(const string &name,boost::shared_ptr<ShpGeometry> oneGeometry);
- boost>shared_ptr
- 详解 boost 库智能指针(scoped_ptr<T> 、shared_ptr<T> 、weak_ptr<T> 源码分析)
- boost>shared_ptr
- Boost.smart_ptr.shared_ptr--2
- 在smart_ptr中处理引用计数时使用swap概念(boost::shared_ptr.hpp)
- boost综合使用<function,bind,thread,mutex,condition_variable,shared_ptr>
- 详解 boost 库智能指针(scoped_ptr<T> 、shared_ptr<T> 、weak_ptr<T> 源码分析)
- Caffe错误boost::python::register_ptr_to_python<boost::shared_ptr<Blob<Dtype> > >();
- [置顶] 从零开始学C++之boost库(一):详解 boost 库智能指针(scoped_ptr<T> 、shared_ptr<T> 、weak_ptr<T> 源码分析)
- [置顶] 从零开始学C++之boost库(一):详解 boost 库智能指针(scoped_ptr<T> 、shared_ptr<T> 、weak_ptr<T> 源码分析)
- Boost源码剖析--<boost/smart_ptr/scoped_ptr.hpp>
- boost>shared_ptr
- boost>shared_ptr
- boost中类似于 shared_array<int> ptr(0) error
- 从零开始学C++之boost库(一):详解 boost 库智能指针(scoped_ptr<T> 、shared_ptr<T> 、weak_ptr<T> 源码分析)
- boost.smart_ptr库简介
- boost 智能指针auto_ptr scoped_ptr shared_ptr scoped_array shared_array 总结
- [5] 智能指针boost::shared_ptr