1#include <iostream>2#include <memory>3 4 using namespacestd;5 classA6 {7 Public:8Ainta)9 {Tencout <<"A () ...."<<Endl; One This->a =A; A } - voidfunc () - { thecout <<"a="<< This->a <<Endl; - } -~A () - { +cout <<"~a () ...."<<Endl; - } + Private: A intA; at }; - //auto_ptr<a> ptr (new A (Ten)); - - classmyautoptr - { - Public: inMyautoptr (A *ptr) - { to This->ptr = ptr;//new A (Ten) + } -~myautoptr () the { * if( This->ptr! =NULL) $ {Panax Notoginseng Deleteptr; - This->ptr =NULL; the } + } AA *operator-() the { + return This-ptr; - } $a&operator*() $ { - return*( This-ptr); - } the Private: -A *ptr;Wuyi }; the - voidtest01 () Wu { - #if0 AboutA * ap =NewATen); $Ap->func (); -(*AP). Func (); - Deleteap; - #endif AAuto_ptr<a> PTR (NewATen)); +Ptr->func (); the(*ptr). Func (); - } $ voidtest02 () the { theMyautoptr my_p (NewATen)); the theMy_p->func ();//My_p.ptr, func () -(*my_p). Func ();//*ptr.func () in } the the intMain () About { the //test01 (); the test02 (); thecout <<Endl; +cout <<"End"<<Endl; -System"Pause"); the return 0;Bayi}
Self-enabling pointer testing and manual implementation