Xiao Budo_PTR has always thought that Auto_Ptr is an easy-to-use thing. Although I have never used it. Until yesterday, I finally used the VC version of Auto_PTR. There are many behaviors that have been puzzled. There is no way, but also re-put the STL reference book It has been studied, and then the VC version of the auto_ptr source code has been studied. It turns out that there is something, summed up some of the following stuff, looking for beginners to help. At the same time, if there is a place I hope you have more advice. Detailed explanation, code annotation! // auto_ptr.h # ifndef auto_ptr__h __ # define auto_ptr__h __ // declaration: // 1. The following source code is for learning only. Any consequences caused during any use of use If you are using, you accept the declaration // Author's Ank Bin Liang // Time 2003.06.21 // Email: Kinglinux@163.com//vc auto_ptr: A written by the AUTO_PTR behavior written according to the VC version Auto_PTRNAMESPACE VC {Template Class Auto_Ptr {Public: Explicit Auto_PTR (T * P = Null): _OWN (p! = 0), _ptr (p) {} public: auto_ptr (const auto_ptr & y): _OWN (Y._WN), // ownership transfer to target _ptr (y.release ()) {} auto_ptr & operator = (const auto_ptr & y) {if (this! = & y) {IF (_PTR! = Y._PTR) // Is the same object {if (_OWN) // has ownership {delete _ptr; _ptr = null;} _oen = y._oen;} else if (y._oen) _oen = True; _ptr = y.release (); // ownership transfer to the target} Return (* this);} ~ auto_ptr () {if (_w) // If you have ownership {delete _ptr; _ptr = null;}} PUBLIC: T & Operator * () Const { Return (* _ptr);} T * Operator -> () const {return (_ptr);} public: t * release () const {((Auto_Ptr *) THIS -> _ o = false; // Has The right to transfer to the target return (_ptr);} private: bool _oen; // Record if there is a right of right t *ptr; // reference pointer};}; // namespace vc // STL auto_ptr: AUTO_PTR behavior written by STL version A auto_ptrnamespace stl {template class auto_ptr {public: explicit auto_ptr (t * p = null): _ PTR (p) {} public: auto_ptr (auto_ptr & y): _ PTR (Y._PTR) ////// The ownership transfer to the target {y._ptr = null; // Release ownership} Auto_PTR & Operator = (const auto_ptr & y) {if (this! = & Y) {if (_ptr) // release The original object {delete _ptr;
_ptr = null;} _ptr = y._ptr; // ownership transfer to the target y._ptr = null; // Release ownership} Return (* this);} ~ auto_ptr () {delete _ptr; // release owned Object} public: t & operator * () const {return (* _PTR);} t * operator -> () const {return (_ptr);} private: t * _ptr; // Pointer to have a pointer with ownership object; }; // Namespace STL // The following version is refer to the << C standard library >> (The C Standard Library // Nicolai M.JOSUTTI THE Jiemeng Translation of September 2002 // Book P222 6.8 A version of the AUTO_PTR of Reference // Ref Auto_Ptr: Namespace Ref {Template Class Auto_Ptr {Public: Explicit Auto_Ptr {Public: Explicit Auto_PTR (T * P = NULL): _ PTR (P), _ Count (New unsigned Long 1)) {} public: auto_ptr (auto_ptr & y): _ PTR (Y._PTR), _ count (y.addref ()) {} // increase reference count auto_ptr & operator = (Auto_Ptr & y) {if (this! = & y) {if (_ptr! = y._ptr) // Is the same object {release (); // release your original ownership, this step _PTR = Y._PTR; _count = y.addref (); // increase the reference count}}} Return (* this);} ~ auto_ptr () {release (); // Release ownership} T & operator * () const {return (* _ptr); } T * Operator -> () const {return (_ptr); Nsigned long * addref (void) // increase the reference count { (* _ count); return (_count);} private: void release (void) {if (- (* _ count) == 0) // Reduce reference Counting and testing the reference count is 0 {delete _ptr; delete _count;} _count = null; // release reference counter _PTR = null; // release ownership} private: t * _ptr; // Reference Object pointer unsigned long * _count {// Reference counter pointer};}; // Namespace Ref // The three versions of the above are simply subjected to popular AUTO_PTR behavior // simple implementation. On the application, use the reference count Auto_PTR is better .// There are many methods for Auto_Ptr's implementation, especially in accordance with different versions // to change Auto_PTR behavior in accordance with different applications, allowing it to make it more suitable for our app. If you can use the base class in your own class library to do a reference count to achieve secure // AUTO_PTR // with reference counting count, and you can use template <>
The method is to load the class plus a reference count to implement // Auto_PTR /////////// // auto_ptr__h __ // below is the test program under VC6 //dog.h#ifNDEF DOG__H __ # Define Dog__h__ #include #include // Test class class dog {public: dog (std :: string name = "noname"): _ name (name) {std :: cout << "Dog :: DOG () << std :: endl;} ~ dog () {std :: cout << "Dog :: ~ DOG () << std :: end1;} public: void bar (void) {std :: cout < <_name << "ALWAYS BARK AT ROSE." << std :: end1;} public: void name (const st :: string & name) {_name = name;} std :: string name (void) {return _name Private: std :: string _name;}; # endif //dog__h__//main.cpp#include #include #include "auto_ptr.h" #include "dog.h" /// test vc auto_ptrvoid Test_vc_fun (void) {std :: cout << "=== Test VC Auto_PTR ===" << std :: endl; vc :: auto_ptr PTOM (New Dog ("Tom")); {VC: : auto_ptr PJACK (PTOM); PTOM = pjack; // 1. pjack-> name ("jack"); pjack-> bark (); pjack-> name ("Tom");} PTOM-> BARK (); // Because there is 1. => Successful, if it removes 1. => Error} /// Test STL Auto_Ptrvoid Test_stl_fun (void) {std :: cout << "=== Test STL AUTO_P Tr === "<< std :: endl; stl :: auto_ptr PTOM (New Dog (" Tom ")); {STL :: Auto_Ptr PJACK (PTOM); Pjack-> Name (" Jack "); PJACK-> bark ();} // ptom-> bark (); // error} /// Test ref auto_ptrvoid test_ref_fun (void) {std :: cout << === test ref auto_ptr == = "<< std :: endl; ref :: auto_ptr PTOM (New Dog (" Tom ")); {ref :: auto_ptr PJACK (PTOM); PJACK-> Name (" Jack ");