Introduction to C++ for Beginners – A Detailed Explanation of Destructors

01 What is a Destructor

A destructor is a member function of a class, named with a tilde followed by the class name, has no return value, and does not accept parameters. A class can only have one destructor. When an object is created, the system automatically calls the constructor for initialization, and similarly, when an object is destroyed, the system automatically calls a function to perform cleanup tasks, such as releasing allocated memory and closing open files; this function is the destructor.

Note: A destructor has no parameters and cannot be overloaded, so a class can only have one destructor. If the user does not define one, the compiler will automatically generate a default destructor.

Introduction to C++ for Beginners - A Detailed Explanation of Destructors

02 What Work Does the Destructor Perform

In the constructor, member initialization is completed before the function body executes, and it is done in the order they appear in the class. In the destructor, the function body executes first, and then the members are destroyed in the reverse order of their initialization.

When a destructor destroys class-type members, it calls the destructor of the member itself; built-in types (including pointers) do nothing, while smart pointers, being class types, will be automatically destroyed.

Introduction to C++ for Beginners - A Detailed Explanation of Destructors

03 When is the Destructor Called

  • Variables are destroyed when they leave their scope.

  • When an object is destroyed, its members are also destroyed.

  • When an element is easily destructible, its elements are also destroyed.

  • For dynamically allocated objects, they are destroyed when the delete operator is used on the pointer pointing to them.

  • For temporary objects, they are destroyed when the full expression that created them ends.

When a derived class is destroyed, the destructor of the derived class is executed first, followed by the base class’s destructor, and so on, in reverse order along the inheritance hierarchy until the end.

Introduction to C++ for Beginners - A Detailed Explanation of Destructors

04 Timing of Destructor Execution

The destructor is called when the object is destroyed, and the timing of the object’s destruction is related to the memory area it resides in. Objects created outside of all functions are global objects, similar to global variables, located in the global data area of the memory partition, and their destructors are called when the program finishes execution. Objects created inside functions are local objects, similar to local variables, located in the stack area, and their destructors are called when the function execution ends. Objects created with new are located in the heap area, and their destructors are called only when delete is used; if delete is not called, the destructor will not be executed. The following example demonstrates the execution of the destructor.

 
  1. #include <iostream>

  2. #include <string>

  3. using namespace std;

  4. class Demo{

  5. public:

  6. Demo(string s);

  7. ~Demo();

  8. private:

  9. string m_s;

  10. };

  11. Demo::Demo(string s): m_s(s){ }

  12. Demo::~Demo(){ cout<<m_s<<endl; }

  13. void func(){

  14. //Local object

  15. Demo obj1(“1”);

  16. }

  17. //Global object

  18. Demo obj2(“2”);

  19. int main(){

  20. //Local object

  21. Demo obj3(“3”);

  22. //Object created with new

  23. Demo *pobj4 = new Demo(“4”);

  24. func();

  25. cout<<“main”<<endl;

  26. return 0;

  27. }

Output:1main32

Introduction to C++ for Beginners - A Detailed Explanation of Destructors

*Disclaimer: This article is compiled from the internet, and the copyright belongs to the original author. If there is any incorrect source information or infringement of rights, please contact us for deletion or authorization matters.

Leave a Comment