Scoped_ptr is a smart pointer similar to auto_ptr in the standard library. It encapsulates the dynamic objects allocated by the new operator on the spine, this ensures that dynamically created objects can be correctly deleted at any time.
However, scoped_ptr has more strict ownership and does not allow transfer. Assignment and copying of scoped_ptr are invalid, so it is more lightweight and fast.
Scoped_ptr reloads operator * () and operator-> () unreference operators * and arrow operators->. Therefore, scoped_ptr objects can be used as pointers. If scoped_ptr saves a null pointer, the behavior of these two operations is not defined.
Scoped_ptr cannot be equal or unequal between two scoped_ptr, between scoped_ptr and the original pointer, or between null pointers. Operator = and operator! = Are declared as private.
The following is a summary of the scoped_ptr class.
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); // never throws explicit scoped_ptr (STD :: auto_ptr <t> P); // never throws ~ Scoped_ptr () // never throws automatically deletes the saved pointer object at the end of its scope to properly recycle the resource void reset (T * p = 0) // never throws deletes the previously saved pointer and then saves the new pointer P. If P is a null pointer, scoped_ptr will not hold any pointer {boost_assert (P = 0 | P! = Px); // catch self-Reset errors 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 // never throws returns the original pointer saved inside scoped_ptr {return PX;} void swap (scoped_ptr & B ); // never throws swap two original pointers saved by scoped_ptr };
Scoped_ptr has two advantages:First, enableCodeIt is clearer and simpler, while simplicity means fewer errors. Second, it does not add additional operations. It ensures security while ensuring efficiency, and can get the same speed as the original pointer.
Example:
# Include <iostream> # include <string> # include <boost/smart_ptr.hpp> using namespace boost; using namespace STD; struct posix_file {posix_file (const char * file_name) {cout <"Open File:" <file_name <Endl ;}~ Posix_file () {cout <"Close file" <Endl ;}; int main () {/* // Note: Don't try to use 'delete' again, // scoped_ptr help us destory resource automatically // scoped_ptr is a pointer-behavior-like object, but it isn't a pointer // even if you use 'delete' it wouldn't work // scoped_ptr do not support assignment and copy // It can only use operator * and- >, SP ++, SP = sp2.etc. are illegal usage here * // * exmple1 */s Coped_ptr <string> Sp (new string ("Hello, ajioy! "); Cout <* sp <Endl; cout <SP-> size () <Endl; /* exmaple2 */scoped_ptr <int> P (New INT); // equal int * P; If (p) {// test P is valid * P = 100; cout <* P <Endl;} p. reset (); // make P null assert (P = 0); If (! P) {cout <"scoped_ptr = NULL" <Endl;} scoped_ptr <posix_file> FP (New posix_file ("/tmp/a.txt ")); // destructor scoped_ptr here // P and FP are deleted automatically}
Running result:
Hello, ajioy!
12
100
Scoped_ptr = NULL
Open File:/tmp/a.txt
Close file
Differences from auto_ptr
The fundamental difference between the two lies in Pointer ownership. Auto_ptr is designed to be able to hand over ownership, while scoped_ptr is the opposite.
Scoped_ptr clearly shows the intent of the original code: it can only be used within the defined scope and cannot be transferred.
Scoped_ptr can be exchanged with auto_ptr in most cases, and the pointer ownership can be obtained from an auto_ptr (at the same time, auto_ptr loses management rights ).
Both of them have defects and cannot be used as container elements, but the reason is different: auto_ptr is because of its transfer semantics, while scoped_ptr does not support copying and assigning values and does not meet the container's requirements for element types.
Example:
# Include <iostream> # include <boost/smart_ptr.hpp> using namespace boost; using namespace STD; int main () {auto_ptr <int> aP (New int (10 )); scoped_ptr <int> Sp (AP); // get original pointer from auto_ptr assert (AP. get () = 0); // The original auto_ptr no longer owns the pointer AP. reset (New int (20); // auto_ptr holds a new pointer cout <* AP <"," <* sp <Endl; auto_ptr <int> AP2; AP2 = ap; // AP2 gets the orginal pointer from AP, transfer ownership assert (AP. get () = 0); // AP no longer owns a pointer scoped_ptr <int> SP2; // SP2 = sp; it won't compile}
Running result:
20, 10