[C++ Study Notes]: Destructor

C++ destructor overview

The C++ destructor is a special member function that has the opposite effect to the constructor. Its name is the class name preceded by a ~ symbol. The destructor is a function that has the opposite effect to the constructor. When the object's life cycle ends, The destructor will be executed automatically.

C++ execution of destructor

If an object is defined in a function, the object should be released when the function is called, and the destructor will be automatically executed before the object is released.

The static local object object is not released when the function call ends, so the destructor is not called. The destructor of the static local object is called only when the main function ends and calls the exit function to end the program.

If a global object is defined, the global object's destructor is called when the program's flow leaves its scope.

If an object is dynamically created using the new operator, when the object is released using the delete operator, the destructor of the object is first called. 

Detailed explanation of C++ destructor

The function of the destructor is not to delete the object, but to complete some cleanup work before canceling the memory occupied by the object, so that this part of the memory can be allocated to new objects by the program.

The destructor does not return any value, has no function type, and has no function parameters, so it cannot be overloaded. A class can have multiple constructors, but only one destructor. 

The role of the destructor is not limited to releasing resources, it can also be used to perform any operation the programmer wishes to perform after the object was last used.

If there is no destructor defined, the C++ compilation system will automatically generate a destructor, but it only has the name and form of the destructor, and actually does nothing. If you want the destructor to execute, it must be in the defined specified in the destructor.

Case: C++ destructor case

#include <iostream>
using namespace std;
class Number
{    public:       void setNumber( double num );       double getNumber( void );       Number(); // This is the constructor declaration       ~Number(); // This is the destructor Declare    private:       float number; }; // Member function definition, including constructor Number::Number(void) {     cout << "I am a constructor" << endl; } Number::~Number(void) {     cout < < "I am a destructor" << endl; } void Number::setNumber( double num ) {     number = num; } double Number::getNumber( void ) {     return number;

















 




 



}
// Main function of the program
int main( )
{    Number num;    num.setNumber(6.0); // Set length    cout << "Student number is:" << num.getNumber() <<endl;    return 0; }




Compile and run results:

I am a constructor. My
student number is: 6.
I am a destructor

.--------------------------------
Process exited after 2.047 seconds with return value 0
Please press any key to continue. . .

Guess you like

Origin blog.csdn.net/Jiangziyadizi/article/details/129538965