Boost里的Smart Pointer
2010-09-30 13:02 xxmplus 阅读(556) 评论(0) 编辑 收藏 举报Boost的智能指针库里有六种指针类模板,这里描述的是shared_ptr。
- 这些指针类模板都有一个模板参数T,T是智能指针所指对象的类型。如果类型T的析构函数和delete操作符会抛出异常,那么智能指针的行为是未定义的。
- 在指针声明的时候,T可以是不完整的类型。但是在指针实例化的时候,T必须是完整的类型。参见checked_delete函数模板。
- 注意,shared_ptr不受上述限制,该因其大部分成员函数都不要求T是完整类型。而scoped_ptr在销毁的时候必须是完整类型。
- shared_ptr指向一个动态分配的对象,被指向的对象可以保证在最后一个指向它的shared_ptr被销毁或重置的时候析构。
- shared_ptr满足C++标准库的定义因此可以用于标准库容器。
- 不要将shared_ptr指向动态分配的数组,你应该用shared_array。
- shared_ptr的技术是引用计数。因此会有循环引用的问题。例如有一个shared_ptr指向A,而A又直接或间接地有一个shared_ptr指向A,那么A的计数为2。销毁第一个shared_ptr会导致A的计数变成1,结果造成A无法销毁的结果。在这种情况下,应该使用weak_ptr。
- shared_ptr对模板参数T没有要求,T可以是不完整的类型,甚至可以是void。
- 只要T*可以隐式转换为U*,那么shared_ptr<T>可以隐式转换为shared_ptr<U>。
- shared_ptr<T>可以隐式转换为shared_ptr<T const>。
- 只要U是T的基类,那么shared_ptr<T>可以隐式转换为shared_ptr<U>。
- shared_ptr是TR1的一部分。
- 在使用shared_ptr时,一定采用这样的形式:shared_ptr<T> p(new Y);
- 避免使用匿名的shared_ptr,因其有可能造成内存泄漏。看这里。
- 如果一个类会由shared_ptr来管理,那么你应该将析构函数保护起来;或者将其私有化,然后定义一个私有的deleter。参见这里。另外这个页面有很多实用的技巧可供参考。
// $ g++ -I<boost headers> -otest test.cpp
#include <iostream> #include <string> #include <boost/shared_ptr.hpp> #include <boost/make_shared.hpp> int main() { //std::string *pstr = new std::string("hello boost!"); //std::cout<<"address \""<<pstr<<"\" is pointing to \""<<*pstr<<"\"\n"; //boost::shared_ptr<std::string> bpstr = boost::make_shared<std::string>(*pstr); boost::shared_ptr<std::string> bpstr(new std::string("hello boost!")); std::cout<<"boost shared_ptr "<<bpstr<<" is pointing to \""<<*bpstr<<"\"\n"; std::cout<<bpstr<<" is"<<(bpstr.unique() ? "" : " not")<<" unique\n"; std::cout<<"assigning bpstr to p...\n"; boost::shared_ptr<std::string> p(bpstr); std::cout<<bpstr<<" is"<<(bpstr.unique() ? "" : " not")<<" unique\n"; std::cout<<p<<" has refcount = "<<p.use_count()<<std::endl; std::string *pstring = p.get(); std::cout<<*pstring<<std::endl; std::cout<<p<<" has refcount = "<<p.use_count()<<std::endl; p.reset(); std::cout<<p<<" has refcount = "<<p.use_count()<<std::endl; std::cout<<bpstr<<" has refcount = "<<bpstr.use_count()<<std::endl; return 0; }