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
// Copy the configuration of the auto_ptr
Source_Object.m_p = 0; //
} // is constructed by auto_ptr
// Destructure .... ~ Auto_PTR () {IF (M_P! = 0) Release ();
// is assigned by auto_ptr
// is assigned by auto_ptr
// Due to the above-mentioned constructor constructor from auto_ptr
// This is confusing :::/ Return Auto_Ptr
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
Auto_PTR
Auto_Ptr
Auto_Ptr
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;
}