C++ Destructors Tutorial


In C++, destructors are special member functions that are called when an object is about to be destroyed. 

The purpose of a destructor is to free up any resources that the object was using, such as memory or file handles, and to perform any final cleanup that is necessary.


Destructors are called automatically by the C++ runtime when an object goes out of scope, or when the delete keyword is used to free the memory allocated for an object created with the new keyword.


Destructors have a specific syntax that is similar to constructors.

 The name of a destructor is the same as the name of the class, but with a tilde (~) character in front of it. 

For example, if a class is named MyClass, the destructor would be named ~MyClass.


Here is an example of a simple class with a constructor and a destructor:


kotlin code


class MyClass

 { 

public: 

MyClass()

 {

 // constructor code here 

~MyClass() 

// destructor code here

 } }; 


The destructor in this example doesn't do anything, but you could imagine that it might free up some resources that the constructor allocated. 

The important thing to note is that the destructor is called automatically when an object of this class is destroyed.


Destructors can be used for a variety of purposes. One common use case is to free up memory that was allocated by the constructor.

 For example, if a constructor allocates memory using the new keyword, the destructor can free that memory using the delete keyword.


Here is an example of a class that allocates memory in the constructor and frees it in the destructor:


cppCopy code


class MyAllocatingClass 

public: 

MyAllocatingClass()

 {

 m_data = new int[100]; 

~MyAllocatingClass()

 { 

delete[] m_data; 

private: int* m_data; 

}


In this example, the constructor allocates an array of 100 integers using the new keyword. 

The destructor then frees that memory using the delete[] keyword.

 This ensures that the memory is properly cleaned up when an object of this class is destroyed.


Destructors can also be used to perform other types of cleanup, such as closing file handles or releasing other resources. 

Here is an example of a class that opens a file in the constructor and closes it in the destructor:


scssCopy code


class MyFileClass

 { 

public: 

MyFileClass(const std::string& filename)

 { 

m_file = fopen(filename.c_str(), "r");

 }

 ~MyFileClass()

 { fclose(m_file); 

private: FILE* m_file; 

}; 


In this example, the constructor opens a file using the fopen() function and stores the file handle in a member variable. 

The destructor then closes the file using the fclose() function. 

This ensures that the file is properly closed when an object of this class is destroyed.


It's important to note that destructors are called in the reverse order of construction. 

This means that if you have a class that contains other objects as member variables, the destructors for those objects will be called before the destructor for the outer class.


Here is an example of a class that contains another class as a member variable:


cppCopy code


class MyOuterClass 

{

 public: 

MyOuterClass() 

m_inner = new MyInnerClass(); 

~MyOuterClass()

 { 

delete m_inner; 

private: 

MyInnerClass* m_inner; 

}; 

class MyInnerClass

 { 

public:

 ~MyInnerClass() 

// inner class destructor code here 

} }


In this example, the MyOuterClass constructor allocates an object of the MyInnerClass class using the new keyword and stores it in a member variable. The MyOuter