boost::scoped_ptr智能指针
2017-04-12 13:18
423 查看
1. 简介
scoped_ptr是boost库提供的很类似auto_ptr的智能指针,它包装了new操作符在堆上分配的动态对象,能够保证动态创建的对象在任何时候都可以被正确地删除。在实现上都是利用了一个栈上的对象去管理一个堆上的对象,从而使得堆上的对象随着栈上的对象销毁时自动删除。但scoped_ptr的所有权更加严格,不能转让,并且永远不能被复制或赋值,一旦scoped_ptr获取了对象的管理权,你就无法再从它那里取回来。因此,在实际编程中是使用scoped_ptr还是auto_ptr,就需要根据它们的不同点进行选择。如果,根据你的设计是否需要转移所有权。如果需要那么就使用auto_ptr,如果不需要那么就使用scoped_ptr。scoped_ptr是一种轻量级的智能指针;使用它不会使你的程序变大或变慢。它只会让你的代码更安全,更好维护。
2. 实现分析
下面我们看下boost::scoped_ptr类的成员函数,熟悉boost::scoped_ptr提供了哪些方法给我们。namespace boost { template<class T> class scoped_ptr // noncopyable { private: T * px; scoped_ptr(scoped_ptr const &); scoped_ptr & operator=(scoped_ptr const &); typedef scoped_ptr<T> this_type; void operator==( scoped_ptr const& ) const; void operator!=( scoped_ptr const& ) const; public: explicit scoped_ptr( T * p = 0 ); ~scoped_ptr(); // never throws void reset(T * p = 0); // never throws T & operator*() const; // never throws T * operator->() const; // never throws T * get() const BOOST_NOEXCEPT; void swap(scoped_ptr & b) BOOST_NOEXCEPT; } template<class T> inline void swap(scoped_ptr<T> & a, scoped_ptr<T> & b) BOOST_NOEXCEPT { a.swap(b); } }
上面的类摘要是从boost_1_63_0\boost\smart_ptr\scoped_ptr.hpp拷贝出来(只拷贝出一部分),从实现上看拷贝构造和赋值构造函数被声明为私有,因此,scoped_ptr是不需要赋值和拷贝的,保证了被它管理的指针不能被转让所有权。
构造函数explicit scoped_ptr( T * p = 0 ):scoped_ptr的构造函数接受一个类型为T*的指针p,创建出一个scoped_ptr对象,并在内部保存指针参数p。p必须是一个new操作符动态分配的结果,或者是个空指针。在构造的时候,不要求T必须是一个完整的类型。当指针p是调用某个分配函数的结果而不是直接调用new得到的时候很有用:因为这个类型不必是完整的,只需要类型T的一个前向声明就可以了。
析构函数~scoped_ptr():scoped_ptr的析构函数会使用delete操作符自动销毁所保存的指针对象,从而正确地回收资源。类型T在被销毁时必须是一个完整的类型。
成员函数void reset(T * p = 0):成员函数reset()的功能是重置scoped_ptr,它删除原来保存的指针,再保存新的指针值p。如果p是空指针,那么scoped_ptr将不持有任何指针。一般情况下reset()不应该被调用,因为它违背了scoped_ptr的本意——资源应该一直由scoped_ptr自己自动管理。
解引用操作符*和箭头操作符->:scoped_ptr重载了引用操作符*和箭头操作符->,以模仿被代理的原始指针的行为,因此可以把scoped_ptr对象如同指针一样使用。如果scoped_ptr保存空指针,那么这两个操作的行为是未定义的。
成员函数swap():可以交换两个scoped_ptr保存的原始指针。
成员函数get():它返回scoped_ptr内部保存的原始指针,可以用在某些必须是原始指针的场景(如底层的c接口)。但使用时必须小心,这将使原始指针脱离scoped_ptr的控制!不能对这个指针做delete操作,否则scoped_ptr析构时会对已经删除的指针再进行删除操作,发生未定义行为。
普通函数swap():这个函数提供了交换两个scoped pointer的内容的更好的方法。之所以说它更好,是因为 swap(scoped1,scoped2) 可以更广泛地用于很多指针类型,包括裸指针和第三方的智能指针。成员函数则只能用于它的定义所在的智能指针,而不能用于裸指针。
3. 使用实例
下面例子演示scoped_ptr智能指针的使用:#include <iostream> #include <boost/scoped_ptr.hpp> using namespace std; class CMyClass { public: explicit CMyClass() { cout << "CMyClass的构造函数" << endl; } ~CMyClass() { cout << "CMyClass的析构函数" << endl; } void MyFunc(void) { cout << "Do Something...." << endl; } }; void Function1(void) { //boost::scoped_ptr是一个简单的智能指针,它 //能保证离开作用域后对象被自动释放,能避免由于 //忘记释放而导致的内存泄漏。 boost::scoped_ptr<CMyClass> spMyClass(new CMyClass()); spMyClass->MyFunc(); } int main(void) { Function1(); return 0; }
运行结果,显示当离开Function1函数的作用域时,对象被自动释放。
CMyClass的构造函数 Do Something.... CMyClass的析构函数
相关文章推荐
- [5]智能指针boost::scoped_ptr
- Boost智能指针:scoped_ptr
- Boost智能指针——scoped_ptr、shared_ptr、weak_ptr、intrusive_ptr
- [4] 智能指针boost::scoped_ptr
- 详解 boost 库智能指针(scoped_ptr<T> 、shared_ptr<T> 、weak_ptr<T> 源码分析)
- Boost智能指针——scoped_ptr
- Boost智能指针——scoped_ptr和shared_ptr
- Boost智能指针之scoped_ptr
- 【Boost】boost库中智能指针——scoped_ptr
- 从零开始学C++之boost库(一):详解 boost 库智能指针(scoped_ptr<T> 、shared_ptr<T> 、weak_ptr<T> 源码分析)
- 几种智能指针的比较(std::auto_ptr、boost::scoped_ptr、boost::shared_ptr、boost::weak_ptr)
- 学习笔记四:boost智能指针:scoped_ptr和shared_ptr
- [置顶] 从零开始学C++之boost库(一):详解 boost 库智能指针(scoped_ptr<T> 、shared_ptr<T> 、weak_ptr<T> 源码分析)
- boost 智能指针auto_ptr scoped_ptr shared_ptr scoped_array shared_array 总结
- 详解 boost 库智能指针(scoped_ptr<T> 、shared_ptr<T> 、weak_ptr<T> 源码分析)
- [置顶] 从零开始学C++之boost库(一):详解 boost 库智能指针(scoped_ptr<T> 、shared_ptr<T> 、weak_ptr<T> 源码分析)
- C++:Boost智能指针——scoped_ptr
- 智能指针boost::scoped_ptr
- boost库在工作(6)作用域智能指针scoped_ptr之五
- 【C++】智能指针的作用,模拟实现auto_ptr,scoped_ptr,shared_ptr,scoped_array,shared_array