C ++ - initialization table, the this pointer, the function normally, destructor, copy constructor, copy assignment (Day5 Continued)

Fourth, the destructor (Destructor.)

public:

  ~ Class name (void) {...} // destructor

1, features destructor

(1) is a special destructor member function name "~ Class Name"

(2) no return type, there is no parameter can not be overloaded (overloaded be necessary and sufficient condition comprising parameters), a class can have a destructor

(3) is responsible for cleaning up resources dynamically allocated objects at construction

class Integer { 

public : 

  Integer ( int Data): M_DATA ( new new  int (Data)) {...} // dynamically allocate a memory object creation
 
  ~ Integer ( void ) () { // is automatically invoked when the object is destroyed 

    Delete M_DATA; 

    M_DATA = NULL; 

  } 

Private : 

  int * M_DATA; 

};

 

2, calls the destructor

(1) When an object is destroyed destructor is automatically invoked

    1) Stack objects out of scope, the destructor is "}" call

    2) heap objects, or delete the delete [] operator calls

(2) if a class is not defined destructor, then the compiler will provide a default constructor

    1) the member variables of the base class type, do nothing

    2) type of member variable classes, call the appropriate analysis destructor members constituting child objects.

3, the process of creating and destroying objects

Creating (1) object

  Memory allocation -> child object constructor call of members (the members of the plurality of sub-objects defined by the sequence constructor) -> the implementation of the class's constructor

(2) object is destroyed

  The implementation of this class destructor -> sub-object calling the destructor member -> release memory

 

Fifth, the copy constructor and copy assignment

1, copy construction

  If a class member variable which contains a dynamically allocated, using the default copy constructor, since a pointer to the same address, so delete this address will be twice as following m_data.

class Integer { 

public : 

  Integer ( int Data): M_DATA ( new new  int (Data)) {...} // dynamically allocate a memory object creation
 
  ~ Integer ( void ) () { // is automatically invoked when the object is destroyed 

    Delete M_DATA; 

    M_DATA = NULL; 

  } 

Private : 

  int * M_DATA; 

};

 

  When using the copy constructor, then the default copy constructor copies of a pointer, and pointers to the same source, and an address, called a shallow copy of this copy. It can lead to data shared by different objects. I.e. the default copy constructor, copy pointer shallow copy. May cause abnormal double free.

  It is necessary to carry out deep copy, i.e., copy data, as follows:

Integer ( const Integer & that) { 

  M_DATA = new new  int ;

   * * = M_DATA that.m_data; // write word or synthetic, may be used to initialize the table 

} 

// can not be written in the following manner, and otherwise the default copy constructor function as 

Integer ( const Integer & that): M_DATA (that.m_data) { 

}

 

Guess you like

Origin www.cnblogs.com/ptfe/p/11252890.html