Auto

zhaozj2021-02-12  174

AUTO_PTR_REF's wonderful (below)

The auto_ptr talks earlier, and its parameter type of its replication operation happens to a very quite reference. So it cannot be handled correctly for the situation below.

Auto_PTR ap1 = auto_ptr (new int (8)); // is a temporary right value on the right

Auto_PTR fun () // a source function generating auto_ptr

{RETURN Auto_Ptr (new int (8))}

Auto_Ptr ap2 (fun ()); // Call the auto_ptr is right value

And this situation is not only legal, it is also very common, we can't reject this kind of use, what should I do? The genius C library designer has thought of this, and the introduction of auto_ptr_ref is to solve this problem. Carefully observe the bottom AUTO_PTR to implement the code, we will see such lines:

/ * Special conversions with auxiliary type to enable copies and assignments * / auto_ptr (auto_ptr_ref rhs) throw (): ap (rhs.yp) {} auto_ptr & operator = (auto_ptr_ref rhs) throw () {// NEW RESET (RHS.YP); RETURN * THIS;

Template Operator auto_ptr_ref () throw () {return auto_ptr_ref (Release ());}

This is the key. For an auto_ptr right value, it is impossible to call a normal assignment operator function and copy constructor, and give an example, for the statement

Auto_PTR ap1 = auto_ptr (new int (8));

First, the temporary object right is acto_ptr (New INT (8)), then use the transformation function template

Template Operator auto_ptr_ref () throw ()

{RETURN Auto_PTR_REF (Release ());

From Auto_Ptr (NEW INT (8)), the member function Release () is called, and then another temporary object right value Auto_PTR_REF (a pointer to the dynamic storage area) is generated by the obtained original pointer. At this time we look at a constructor again

Auto_PTR (auto_ptr_ref rhs) throw (): AP (rhs.yp) {}

Its parameter type is not a reference, which uses the pass value, so you can accept the right value. At this time, call auto_ptr_ref The default generated replication constructor (Copy Constructor), using the last AUTO_PTR_REF < INT> Temporary object right as the quota, generated RHS, then auto_ptr_ref Temporary object right automatic sectation ends life, the following AP (rhs.yp) completes the last job. Our entire adventure process is over. It is best to read the chapters used by Auto_PTR in "The C Standard Library". Such AUTO_PTR use and all design principles are no longer mysterious.

Appendix: Auto_PTR's implementation code, from Nicolai M. Josuttis

/ * Class auto_ptr * - improved standard conforming implementation * / namespace std {// auxiliary type to enable copies and assignments (now global) template struct auto_ptr_ref {Y * yp; auto_ptr_ref (Y * rhs): yp (rhs ) {}}; template class auto_ptr {private: T * ap; // refers to the actual owned object (if any) public: typedef T element_type; // constructor explicit auto_ptr (T * ptr = 0) throw (): AP (PTR) {} // Copy Constructors (with Implicit Conversion) // - NOTE: NONCONSTANT Parameter Auto_Ptr (Auto_Ptr & RHS) Throw (): AP (RHS.RELEASE ()) {} Template auto_ptr (AUTO_PTR & RHS) throw (): AP (RHS.RELEASE ()) {} // Assignments // - NOTE: NONCONSTANT Parameter Auto_Ptr & Operator = (Auto_PTR & RHS) throw () {RESET ( RHS.RELEASE ()); Return * this;} Template auto_ptr & operator = (auto_ptr & r Hs) throw () {reset (rhs.release ()); return * this;} // destructor ~ auto_ptr () throw () { Delete ap; AP;} // Release Ownership T * Release () throw () {t * tmp (AP); AP = 0; Return TMP;

} // Reset Value Void Reset (T * PTR = 0) throw () {if (ap! = Ptr) {delete ap; ap = PTR;}} / * special conversidies and assignments * / auto_ptr (AUTO_PTR_REF RHS) throw (): AP (rhs.yp) {} auto_ptr & operator = (auto_ptr_ref rhs) throw () {// new reset (rhs.yp); return * this;} template < Class Y> operator auto_ptr_ref () throw () {return auto_ptr_ref ();} template Operator auto_ptr () throw () {return auto_ptr (Release ));}};} Wu Tong writes in 2003.6.16

Recently modified 2003.6.16

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

New Post(0)