Smart Pointer

zhaozj2021-02-11  202

Smart Pointer: The smart pointer is actually an object A. It has a model member variable m_p. Use this A to manage M_P, through this management mechanism, memory leakage due to New. Smart pointer Objects are like pointers when using. At the same time, it also has all the features of the general object. If you want to pay attention to the following: 1, the value between the objects: If there is a = B, it is assigned to the value, the first quitter is itself Assignment. If not, release the memory space of the A object member pointer, then assign the member pointer in the B in the member pointer of the A, then place the member pointer of B in the B, which avoids multiple pointers to point to one Memory space generates a multiple destructure error. Therefore, the assignment function of the overload is generally as follows: operator = (Test & Source_Object); this is not a reference to the const object, this is because the member pointer to modify the Source_Object is Empty. 2, there is also a similar situation with Operator = for Copy Constroctor. Just do not release the memory space of the A member pointer, .3, about Member Template problem, it is often used to compatibility between template objects, Copy Constroctor We can see it as a template class (a class). For example: auto_ptr , this is a class name: in VC7.0, the definition and implementation of the member template must be placed in the definition of the class. Note: MEMBER TEMPLATE Can't be overloaded in Static or Virtual4: -> * This purpose is to mimic the object to the behavior of the pointer. As follows: t * operator -> () {return m_p;}

Special attention: Object-> m_p; == (Object.operator -> ()) -> m_p;

T & operator * () {return * m_p;}

The following program demonstrates the basic use of Smart Pointer: #include "iostream" using namespace std; template class auto_ptr {public: auto_ptr {public: auto_ptr (t * p = 0) {m_p = p;}

// Copy the configuration of the auto_ptr by auto_ptr . Auto_PTR (Auto_Ptr & Source_Object) {m_p = (t *) (Source_Object.get ());

Source_Object.m_p = 0; //

} // is constructed by auto_ptr . Template auto_ptr (auto_ptr & source_object) // This object is a reference to non-Const objects. {M_p = (t *) (Source_Object.get ()); Source_Object.m_p = 0; //}

// Destructure .... ~ Auto_PTR () {IF (M_P! = 0) Release ();

// is assigned by auto_ptr object. Auto_PTR & Operator = (Auto_Ptr & Source_Object) {Release (); m_p = (t *) (Source_Object.get ()); return * THIS;

// is assigned by auto_ptr object. Template auto_ptr & operator = (auto_ptr & source_object) {Release (); m_p = (t *) (Source_Object.get) ))); Return * this;} // delete m_p; void release () {if (m_p! = 0) delete m_p; m_p = 0;} t * get () {return m_p;} // from auto_ptr Convert to auto_ptr

// Due to the above-mentioned constructor constructor from auto_ptr . So, in fact, it is not necessary to give the following implicit type conversion function. // 2 VC does not support this implicit conversion characteristic Template Operator Auto_Ptr () // If the M and T types are inconsistent, automatic type conversion will not be performed. Such as: auto_ptr with auto_ptr {return auto_ptr (m_p );

// This is confusing :::/ Return Auto_Ptr (m_p); this is the Writing method in More Effective C , when this statement is performed, the object of a new auto_ptr is constructed, and the THIS object M_P is not released. The m_p of the two object points to a total variable, ??? // should be like this: // Temp Auto_Ptr (Get ()); // Release (); // Return Temp;}

T * Operator -> () {return m_p;}

Const T * Operator -> () const {return m_p;} // Note that T &, this can achieve the left value. T & operator * () {return * m_p;}

Const T & Operator * () const {return * m_p;} public: t * m_p;};

Class test {public: test (int data) {m_data = data;} int & operator -> () {return m_data;}

Operator int () {return m_data;}

PUBLIC:

INT M_DATA;

Class a {public: a (int data) {m_data = data;} private: int m_data

}

Class B: Public a {public: b (int data): a (Data * 2), m_data (data) {;} private: int m_data;

}

Class C: Public a {public: c (int DATA): a (Data * 3), m_data (data) {;} private: int m_data;

}; void test_member_template (const auto_ptr & Source_Object) // must be added to Const because the object is implicitly converted into a temporary object. The survival period of the temporary object will not be determined by the programmer, its value cannot be Modify. {// source_Object.get ();

Void test_member_template2 (const auto_ptr & source_Object) // must be added to Const because the object is implicitly converted to the object as a temporary object. The survival period of the temporary object will not be determined by the programmer, its value cannot be modified. {; Void main () {auto_ptr Object2 (new c (3)); test_member_template (Object2); // will call the Member Template constructor to construct an Auto_PTR object. Note that after the function is executed, it will cause Object2. Getting to an empty shell object. To avoid this implicit template object structure.

Auto_PTR Object (New Test (15)); int DATA = Object.operator -> () -> m_data; // Equivalent: DATA = Object.operator -> () -> Operator -> (); Cout << data;

Auto_Ptr Object_int (New Int (999));

Auto_Ptr Object_int_2 (New Int (333));

Object_int_2 = Object_int; // Operator =

Object_float = Object_int; // Operator = by Member Template

TEST_MEMBER_TEMPLATE2 (Object_INT); // Copy Constructionor

TEST_MEMBER_TEMPLATE2 (Object_float); // Call Constructor By Member Template. // Note that after the function is executed, it will cause Object2 to become an empty shell object. To avoid this implicit template object constructor. Cout << * Object_float << ENDL ; Auto_Ptr> Object_2 = Object_float; // Call COPY CONSTRUCTOR COUT << * Object_2 << endl;

}


New Post(0)