Scoped_ptr is a smart pointer that encapsulates the pointer to the memory on the heap. It restricts pointer ownership and cannot transfer pointer ownership. Once scoped_ptr obtains the pointer management right, it will no longer be released and cannot be retrieved from it, just as scope means, pointer intelligence is used in the scope and cannot be transferred out. Once it leaves the scope of scoped_ptr, it will call its destructor to release the pointer without manual release. The source code is as follows:
# Ifndef boost_smart_ptr_scoped_ptr_hpp_included # define boost_smart_ptr_scoped_ptr_hpp_included // (c) Copyright Greg Colvin and beman Dawes 1998,199 9. // copyright (c) 2001,200 2 Peter DIMOV /// distributed under the boost software license, Version 1.0. (See // accompanying file license_00000.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 # endifnamespace boost {// debug hooks # If defined (custom) void sp_scalar_constructor_hook (void * P ); void sp_scalar_destructor_hook (void * P ); # Endif // scoped_ptr mimics a built-in Pointer handle t that it guarantees deletion // of the object pointed to, either on struction 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; // Original Ecological pointer // declare the copy constructor and value assignment operator as private, it cannot be used outside the class. Transfer scoped_ptr (scoped_ptr const &); scoped_ptr & operator = (scoped_ptr const &); typedef scoped_ptr <t> this_type; // The reload is private, comparison of void operator = (scoped_ptr const &) const; void Operator! = (Scoped_ptr const &) const; public: typedef t element_type; // display constructor. It must be displayed that the call to explicit scoped_ptr (T * p = 0): Px (P) // never throws {# If defined (SUCCESS) boost: sp_scalar_constructor_hook (PX); # endif} # ifndef boost_no_auto_ptr // constructor when the parameter is auto_ptr // P. release () sets auto_ptr to null and returns its internal pointer explicit scoped_ptr (STD: auto_ptr <t> P) boost_no1_t: Px (P. release () {# If defined (boost_s P_enable_debug_hooks) boost: sp_scalar_constructor_hook (PX); # endif} # endif // destructor, destructor, and releases memory ~ 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 // convert P to scoped_prt, and then call swap to switch this_type (P ). swap (* This);} // reload the unreferenced object, returns the object T & operator * () const // never throws {boost_assert (PX! = 0); return * PX;} // overload->, returns the pointer T * operator-> () const // never throws {boost_assert (PX! = 0); Return PX;} // returns the pointer T * Get () const boost_no1_t {return PX ;} // implicit conversion to "bool" # include <boost/smart_ptr/detail/operator_bool.hpp> // exchange two pointers void swap (scoped_ptr & B) boost_no1_t {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_no1_t {return p. get () = 0;} template <class T> inline bool operator = (boost: detail: sp_nullptr_t, scoped_ptr <t> const & P) boost_no1_t {return p. get () = 0;} template <class T> inline bool Operator! = (Scoped_ptr <t> const & P, boost: detail: sp_nullptr_t) boost_no1_t {return P. Get ()! = 0;} template <class T> inline bool Operator! = (Boost: detail: sp_nullptr_t, scoped_ptr <t> const & P) boost_no1_t {return P. Get ()! = 0 ;}# endiftemplate <class T> inline void swap (scoped_ptr <t> & A, scoped_ptr <t> & B) boost_no1_t {. 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_no1_t {return p. get () ;}// namespace boost # endif // # ifndef boost_smart_ptr_scoped_ptr_hpp_includedScoped_ptr is different from auto_ptr: auto_ptr can transfer the right to use. During the transfer, the previous pointer loses the right to use. Scoped_ptr does not allow the transfer of the use right.
Scoped_ptr and auto_ptr cannot be placed in containers. auto_ptr only allows one auto_prt to use pointers, while scoped_ptr does not allow transfer of use rights, does not support copying and copying, and does not meet the requirements of containers on elements.
Boost source code learning --- scoped_ptr.hpp