More Effective C ++ Item 2: A Auto

zhaozj2021-02-16  56

More Effective C preface, guide and attached 1 (Houjie Translation), and the number of calculated items in C "and" Operator -> * for smart indicators, originally published in programmer magazine) , Can be downloaded at Houjie's site.

An Auto_PTR implements instances Items M9, M10, E26, E31, and E32 demonstrate the unusual role of the Auto_PTR template class. Unfortunately, there are very few compilers to provide a "correct" implementation (Note 1). Items M9 and M28 roughly describe how you implement one, but there is a more detailed version when you engage in actual projects. Here is the implementation of two Auot_PTRs. The first version of the documentation has implemented all the member functions outside the class interface and implements all member functions outside the class definition. The second version will be implemented in the definition of all members functions. In style, the second implementation is not as first as it is not separated from the implementation from the implementation. However, Auto_PTR is just a simple class, so the second implementation is much more clear than the first.

This is an Auto_PTR template with special interface declaration: Template class auto_ptr {public: explicit auto_ptr (t * p = 0); // Item M5 has "explicitfor" // Description Template // copy Constructor member template auto_ptr (auto_ptr & rhs); // (see Item M28): // Initialize a new auto_ptr object ~ Auto_Ptr (); Template // Assignment operation member template auto_ptr & // (see Item M28): Operator = (Auto_Ptr & rhs); // Assigning another type-compatible // auto_ptr object to assign it T & Operator * () Const; / / See Item M28 T * Operator -> () const; // See item M28 T * get () const; // Return to the // Current value of the inclusive pointer T * Release (); // Abandon the inclusion Pointer // Ownership, // and return its current value void reset; // Remove the enclosure pointer, // Get the ownership of the pointer P: T * Pointee; Template // Let all Auto_PTR class Friend class auto_ptr ; // Become a friend}; template inline auto_ptr :: auto_ptr (t * p): pointee (p) {} template inline auto_ptr :: auto_ptr (auto_ptr & rhs): Pointee (rhs.release ()) {} template inline auto_ptr () {delete Pointee

} Template template inline auto_ptr & auto_ptr :: operator = (auto_ptr & r Hs) {if (this! = & r HS) RESET (rhs.Release ()); Return * this;} template inline t & auto_ptr :: operator * () const {return * pointee;} template inline t * auto_ptr :: Operator -> () const { Return Pointee;} template inline t * auto_ptr :: get () const {return pointee;} template inline t * auto_ptr :: release () {t * OldPointee = Pointee Pointee = 0; returnalpointee;} template inline void auto_ptr :: reset (t * p) {if (Pointee! = P) {delete Pointee; Pointee = P;}} This is all functions Define the Auto_PTR template in the class definition. As you can see, it won't mess with the brain: Template class auto_ptr {public: explicit auto_ptr (t * p = 0): Pointee (p) {} template auto_ptr (auto_ptr ) RHS): Pointee (RHS.RELEASE ()) {} ~ auto_ptr () {delete pointee;} template auto_ptr & operator = (Auto_Ptr & rhs) {f (this! = & r HS) RETURN * this;} t & operator * () const {return * pointee;} t * operator -> () const {return pointee;} t * get () const {return Pointee;} T * release () {t * OldPointee = Pointee; Pointee = 0; returnalpointee;} void reset (t * p = 0) {if (Pointee! = P) {Delete Pointee; Pointee = P;}} private: T * Pointee; Template friend class auto_ptr ;}; If you use the compiler, "Explicit" is not supported, you can safely use #define to cancel its existence: #define explicit This will not cause auto_ptr Any function is weakened, but the slight security is weakened. See Item M5 for details.

转载请注明原文地址:https://www.9cbs.com/read-26729.html

New Post(0)