A detailed explanation of the destructor function _c language in C + + programming

Source: Internet
Author: User
Tags ming

C + + destructor

When an object is created, the system automatically calls the constructor to initialize the work, as well as the system automatically calls a function to clean up when the object is destroyed (for example, the various resources consumed when the object is created), which is called a destructor.

A destructor (destructor) is also a special member function that does not have a return value, does not require a user call, but is automatically executed when the object is destroyed. Unlike constructors, the name of the destructor is preceded by a "~" symbol in front of the class name.

Note: Destructors have no parameters and cannot be overloaded, so a class can have only one destructor. If the user does not have a definition, the compiler is automatically generated.

Examples of destructor functions:

#include <iostream>
using namespace std;
Class student{
private:
  char *name;
  int age;
  float score;
Public:
  //Constructor
  Student (char *, int, float);
  Destructors
  ~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;
}
The Age of Void Student::say () {
  cout<<name<< is "<<age<<" and the result is "<<score<<endl;}".
int main () {
  Student stu1 ("Xiaoming", 90.5f);
  Stu1.say ();
  
  Student stu2 ("Li Lei");
  Stu2.say ();
  
  Student stu3 ("Wang Yu", 80.5f);
  Stu3.say ();
  cout<< "Main function is about to end" <<endl;
  
  return 0;
}

Run Result:

Xiao Ming's age is 15, the result is 90.5
Li Lei's age is 16, the result is
Wang Yu age is 16, the result is 80.5
main function is about to run the end
Wang Yu goodbye
lilei goodbye
Xiao Ming goodbye

As you can see, the destructor is executed before the end of the main function, and the call order and constructor are just the opposite, and for convenience of memory, we can interpret it as a stack, first in and out.
The order in which destructors are executed is reversed.
Destructors are executed before the object is destroyed, and to know when the destructor is invoked, you must first know when the object is destroyed.

An object can be considered a variable defined by a data type such as a class, and many of its attributes are the same as ordinary variables, such as scopes, lifecycles, and so on. It can be inferred that the time of destruction of the object is the same as that of the ordinary variable.

To sum up, there are several situations:
1 if an object (auto local variable) is defined in a function, when the function ends, the object is destroyed and the destructor is automatically executed before the object is destroyed.

2 The static local object does not destroy at the end of the function call, and therefore does not call the destructor, and calls the static local object's destructor only at the end of the program, such as when the main function ends or calls the Exit function.

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

4 If an object is dynamically created with the new operator, the destructor of the object is called first when the object is disposed with the delete operator.

Note: The function of a destructor is not to delete an object, but to do some cleanup before undoing the memory occupied by the object, so that this part of memory can be allocated to the new object for use.

Order of C + + call constructors and destructors
When you use constructors and destructors, you need to pay special attention to their invocation time and order of invocation. Under normal circumstances, the order in which the destructor is invoked is exactly the opposite of the order in which the constructor was invoked: the first called constructor, and its corresponding (in the same object) destructor is finally invoked, and the last constructor called, and its corresponding destructor is first invoked. As shown in example 9.5, the destructor of the STUD2 is executed first, and the destructor of the STU1 is executed.

Can be denoted as follows: first constructed after the destructor, after the construction of the first destructor, it is equivalent to a stack, advanced and out.

However, this principle is not dealt with under any circumstances. objects can be defined in different scopes and can have different storage categories. These can affect the timing of calling constructors and destructors.

Here's a summary of when to call the constructors and destructors:
1 objects defined in the global scope (that is, objects defined outside of all functions) whose constructors are called before all functions in the file, including the main function, are executed. However, if there are multiple files in a program, and global objects are defined in different files, the execution order of the constructors for those objects is undefined. The destructor is called when the main function completes or the Exit function is called (at which point the program terminates).

2 If a local automatic object is defined (for example, an object is defined in a function), its constructor is called when the object is created. If the function is called more than once, the constructor is called every time the object is established. The destructor is called first when the function call ends and the object is freed.

3 If you define a static (static) local object in the function, the constructor is invoked only once when the program first calls this function to establish an object, the object is not freed at the end of the call, and therefore the destructor is not called, and the destructor is called only when the main function ends or the Exit function ends.

For example, two objects are defined in a function:

VOID Fn () {
  Student stud1;//define automatic local object
  static Student stud2;//define Static local objects
}


When the FN function is invoked, the Stud1 constructor is called first, then the Stud2 constructor is called, and the Stud1 is released (because it is an automatic local object) at the end of the FN call, so the STUD1 destructor is invoked. The STUD2 is a static local object that is not freed at the end of the FN call and therefore does not call the Stud2 destructor. The Stud2 destructor is invoked until the program finishes releasing Stud2. You can see that STUD2 is called after the constructor, but does not call its destructor first. The reason for this is that two objects have different storage categories and different life cycles.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.