scoped_ptr是一个智能指针,包装了指向堆上内存的指针。它对指针所有权加以限制,不能转让指针所有权,一旦scoped_ptr获取了指针的管理权,便不再释放,无法再从其取回来,就像scope意思一样,指针智能在作用域使用,不能转让出去。一旦离开scoped_ptr的作用域,将调用它的析构函数,释放指针,不用手动释放。下面是它的源代码:
#ifndef BOOST_SMART_PTR_SCOPED_PTR_HPP_INCLUDED #define BOOST_SMART_PTR_SCOPED_PTR_HPP_INCLUDED // (C) Copyright Greg Colvin and Beman Dawes 1998, 1999. // Copyright (c) 2001, 2002 Peter Dimov // // Distributed under the Boost Software License, Version 1.0. (See // accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) // // http://www.boost.org/libs/smart_ptr/scoped_ptr.htm // #include <boost/config.hpp> #include <boost/assert.hpp> #include <boost/checked_delete.hpp> #include <boost/smart_ptr/detail/sp_nullptr_t.hpp> #include <boost/detail/workaround.hpp> #ifndef BOOST_NO_AUTO_PTR # include <memory> // for std::auto_ptr #endif namespace boost { // Debug hooks #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS) void sp_scalar_constructor_hook(void * p); void sp_scalar_destructor_hook(void * p); #endif // scoped_ptr mimics a built-in pointer except that it guarantees deletion // of the object pointed to, either on destruction of the scoped_ptr or via // an explicit reset(). scoped_ptr is a simple solution for simple needs; // use shared_ptr or std::auto_ptr if your needs are more complex. 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: typedef T element_type; //显示构造函数,必须显示调用 explicit scoped_ptr( T * p = 0 ): px( p ) // never throws { #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS) boost::sp_scalar_constructor_hook( px ); #endif } #ifndef BOOST_NO_AUTO_PTR //参数为auto_ptr时的构造函数 //p.release()设auto_ptr为NULL,返回其内部指针 explicit scoped_ptr( std::auto_ptr<T> p ) BOOST_NOEXCEPT : px( p.release() ) { #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS) boost::sp_scalar_constructor_hook( px ); #endif } #endif //析构函数,析构对象,释放内存 ~scoped_ptr() // never throws { #if defined(BOOST_SP_ENABLE_DEBUG_HOOKS) boost::sp_scalar_destructor_hook( px ); #endif boost::checked_delete( px ); } void reset(T * p = 0) // never throws { BOOST_ASSERT( p == 0 || p != px ); // catch self-reset errors //把p转换为scoped_prt,再调用swap交换 this_type(p).swap(*this); } //重载解引用,返回指针指向的对象 T & operator*() const // never throws { BOOST_ASSERT( px != 0 ); return *px; } //重载->,返回指针 T * operator->() const // never throws { BOOST_ASSERT( px != 0 ); return px; } //返回指针 T * get() const BOOST_NOEXCEPT { return px; } // implicit conversion to "bool" #include <boost/smart_ptr/detail/operator_bool.hpp> //交换两个指针 void swap(scoped_ptr & b) BOOST_NOEXCEPT { T * tmp = b.px; b.px = px; px = tmp; } }; #if !defined( BOOST_NO_CXX11_NULLPTR ) template<class T> inline bool operator==( scoped_ptr<T> const & p, boost::detail::sp_nullptr_t ) BOOST_NOEXCEPT { return p.get() == 0; } template<class T> inline bool operator==( boost::detail::sp_nullptr_t, scoped_ptr<T> const & p ) BOOST_NOEXCEPT { return p.get() == 0; } template<class T> inline bool operator!=( scoped_ptr<T> const & p, boost::detail::sp_nullptr_t ) BOOST_NOEXCEPT { return p.get() != 0; } template<class T> inline bool operator!=( boost::detail::sp_nullptr_t, scoped_ptr<T> const & p ) BOOST_NOEXCEPT { return p.get() != 0; } #endif template<class T> inline void swap(scoped_ptr<T> & a, scoped_ptr<T> & b) BOOST_NOEXCEPT { a.swap(b); } // get_pointer(p) is a generic way to say p.get() template<class T> inline T * get_pointer(scoped_ptr<T> const & p) BOOST_NOEXCEPT { return p.get(); } } // namespace boost #endif // #ifndef BOOST_SMART_PTR_SCOPED_PTR_HPP_INCLUDED
scoped_ptr与auto_ptr不同的是:auto_ptr可以转让使用权,在转让的时候,上一个指针失去使用权。而scoped_ptr不允许转让使用权。
scoped_ptr和auto_ptr都不能放在容器,auto_ptr是因为它只允许一个auto_prt使用指针,而scoped_ptr是因为不允许转让使用权,不支持拷贝和复制,不符合容器对元素的要求。
时间: 2024-10-14 12:11:08