Home>

C++ destructor

When the object is created, the system will automatically call the constructor for initialization.Similarly, when the object is destroyed, the system will automatically call a function to clean up (such as recycling various resources consumed when creating the object). This function is called a destructor.

Destructor is also a special member function,No return value,No user calls are required,Instead, it is performed automatically when the object is destroyed.Unlike the constructor,The name of the destructor is preceded by a "~" symbol.

Note:the destructor has no parameters,Cannot be overloaded,So a class can only have one destructor.If not defined by the user,Then the compiler will generate it automatically.

Examples of destructors:

#include<iostream>
using namespace std;
class student {
private:
  char * name;
  int age;
  float score;
public:
  //Constructor
  student (char *, int, float);
  //destructor
  ~ student ();
  //Ordinary member function
  void say ();
};
student ::student (char * name1, int age1, float score1):name (name1), age (age1), score (score1) ()
student ::~ student () {
  cout<name<""goodbye"<endl;
}
void student ::say () {
  cout<name<"'s age is "<age<"and the grade is"<score<endl;
}
int main () {
  student stu1 ("Xiao Ming", 15, 90.5f);
  stu1.say ();
  student stu2 ("Li Lei", 16, 95);
  stu2.say ();
  student stu3 ("Wang Shuang", 16, 80.5f);
  stu3.say ();
  cout<""main function is about to end"
  return 0;
}

operation result:

Xiao Ming's age is 15 and his grade is 90.5
Li Lei's age is 16 and his grade is 95
Wang Shuang's age is 16 and his grade is 80.5
main function is about to end
Goodbye Wang Shuang
Goodbye Li Lei
Xiaoming bye

As can be seen,The destructor is executed before the end of the main function.And the calling order is the opposite of the constructor,For the convenience of memory,We can understand it as a stack,First in, last out.

Why the destructor's execution order is reversed.

The destructor is executed before the object is destroyed;To know when the destructor is called,It is necessary to know when the object is destroyed.

Objects can be thought of as variables defined by data types such as classes,Many of its characteristics are the same as ordinary variables,For example, scope, life cycle, etc.It can be inferred from this,Objects are destroyed at the same time as ordinary variables.

To sum it up,There are several situations:

1) If an object (auto local variable) is defined in a function, when the function ends,The object will be destroyed,The destructor is automatically executed before the object is destroyed.

2) The static local object is not destroyed at the end of the function call.So it doesn't call the destructor,The destructor of a static local object is called only at the end of the program, such as when the main function ends or the exit function is called.

3) If a global object is defined,The destructor of this global object is also called only at the end of the program.

4) If an object is created dynamically using the new operator,When the object is released with the delete operator,The object's destructor is called first.

Note:The role of the destructor is not to delete the object,Instead, do some cleanup work before undoing the memory occupied by the object,Make this part of memory available for new objects.

C++ order of calling constructor and destructor

When using constructors and destructors,Pay special attention to the timing and order of calls to them.under normal circumstances,The order in which the destructors are called is exactly the reverse of the order in which they were called:Its corresponding (in the same object) destructor is finally called,And the last constructor called,Its corresponding destructor is called first.As shown in Example 9.5, first execute the destructor of stud2,Then execute the destructor of stu1.

It can be abbreviated as:construct first and then destruct,Post-structuring first destructuring,It is equivalent to a stack,First in, last out.

However, this is not the case in all cases.Objects can be defined in different scopes,There can be different storage categories.These affect the timing of calling constructors and destructors.

Here is a summary of when the constructor and destructor are called:

1) For objects defined in the global scope (that is, objects defined outside all functions), its constructor is called before all functions in the file (including the main function) are executed.But if there are multiple files in a program,Global objects are defined in different files,The execution order of the constructors of these objects is undefined.When the main function finishes executing or the exit function is called (the program terminates at this time), the destructor is called.

2) If you define a local automatic object (such as defining an object in a function), its constructor is called when the object is created.If the function is called multiple times,The constructor is called every time an object is created.The destructor is called when the function call ends and the object is released.

3) If a static (static) local object is defined in the function,The constructor is called only once when the program first calls this function to create an object,The object is not released at the end of the call,So it doesn't call the destructor,Only when the main function ends or the exit function is called to end the program,Only call the destructor.

For example, two objects are defined in a function:

void fn () {
  student stud1;//Define automatic local objects
  static student stud2;//Define a static local object
}

When calling the fn function, first call the constructor of stud1,Then call the constructor of stud2,At the end of the fn call,stud1 is to be released (because it is an automatic local object), so the destructor of stud1 is called.And stud2 is a static local object,Is not released at the end of the fn call,So the destructor of stud2 is not called.Until the end of the program releases stud2, the destructor of stud2 is not called.You can see that stud2 is called after the constructor,But its destructor is not called first.The reason is that the two objects have different storage categories and different life cycles.

c
  • Previous ASPNET MVC3 teaches you how to build the Web
  • Next Javascript countdown to product spike (time synchronized with server time)