Boost smart pointer -- scoped_ptr
Source: http://www.cnblogs.com/TianFang/archive/2008/09/15/1291050.html
Boost: scoped_ptrAndSTD: auto_ptrSimilarly, it is a simple smart pointer that can ensure that the object is automatically released after it leaves the scope. BelowCodeDemonstrate the basic application of the pointer:
# Include <String> # Include <Iostream> # Include <Boost/scoped_ptr.hpp> ClassImplementation { Public: ~ Implementation () {STD: cout <"Destroying implementation \ n";} VoidDo_something () {STD: cout <"Did something \ n";} }; VoidTest () { Boost: scoped_ptr <implementation> impl (NewImplementation ()); Impl-> do_something (); } VoidMain () { STD: cout <"Test begin... \ n"; Test (); STD: cout <"Test end. \ n"; } |
The output result of this Code is:
Test begin... Did something Destroying implementation Test end. |
You can see that whenImplementationLeave the class openImplThe scope is automatically deleted, so that you do not forget to manually call it.DeleteThis causes memory leakage.
Boost: scoped_ptr features:
The implementation of Boost: scoped_ptr is very similar to STD: auto_ptr. It uses the objects on a stack to manage objects on a stack, so that the objects on the stack are automatically deleted as the objects on the stack are destroyed. The difference is that boost: scoped_ptr has more strict limits-it cannot be copied. This means that the boost: scoped_ptr pointer cannot be converted to its ownership.
- Cannot change ownership
Boost: The object lifecycle managed by scoped_ptr is limited to only one interval (between "{}" where the pointer is located) and cannot be passed out of the range, which means boost :: scoped_ptr objects cannot be returned as function values (STD: auto_ptr can ).
- Cannot share ownership
This is similar to STD: auto_ptr. This feature makes the pointer easy to use. On the other hand, it also causes weak functionality-it cannot be used in STL containers.
- Cannot be used to manage array objects
Because boost: scoped_ptr is passedDeleteTo delete the managed object, and the array object mustDeletep []So boost: scoped_ptr cannot manage array objects. To manage array objects, use the boost: scoped_array class.
Common Operations of Boost: scoped_ptr:
It can be simplified as follows:
NamespaceBoost { Template<TypenameT>ClassScoped_ptr: noncopyable { Public: ExplicitScoped_ptr (T * p = 0 ); ~ Scoped_ptr (); VoidReset (T * p = 0 ); T &Operator*()Const; T *Operator-> ()Const; T * Get ()Const; VoidSwap (scoped_ptr & B ); }; Template<TypenameT> VoidSwap (scoped_ptr <t> & A, scoped_ptr <t> & B ); } |
Its common operations are as follows:
| member functions |
function |
Operator*() |
Access the members of the managed object in Reference Form |
Operator-> () |
Access the members of the managed object as pointers |
Get () |
Release managed objects and manage another object |
Swap (scoped_ptr & B) |
Exchange two boost: Objects managed by scoped_ptr |
The following test code demonstrates the basic usage of these functions.
# Include <String> # Include <Iostream> # Include <Boost/scoped_ptr.hpp> # Include <Boost/scoped_array.hpp> # Include <Boost/config. HPP> # Include <Boost/detail/lightweight_test.hpp> VoidTest () { // Test scoped_ptr with a built-in type Long* Lp =New Long; Boost: scoped_ptr <Long> Sp (LP ); Boost_test (sp. Get () = LP ); Boost_test (Lp = sp. Get ()); Boost_test (& * sp = LP ); * Sp = 1234568901l; Boost_test (* sp = 1234568901l ); Boost_test (* Lp = 1234568901l ); Long* Lp2 =New Long; Boost: scoped_ptr <Long> SP2 (lp2 ); Sp. Swap (SP2 ); Boost_test (sp. Get () = lp2 ); Boost_test (sp2.get () = LP ); Sp. Reset (null ); Boost_test (sp. Get () = NULL ); } VoidMain () { Test (); } |
Select boost: scoped_ptr and STD: auto_ptr:
boost: scoped_ptr and STD: auto_ptr have similar functions and operations. how to select between them depends on whether the ownership of the managed object needs to be transferred (such as whether to be returned as a function) . If this is not required, you can use boost: scoped_ptr to enable the compiler to perform more rigorous checks to find some incorrect assignment operations.