Demystifying C++ Destructors - CSU1287 - Shoolini U

Destructors

Destructors

In C++, a destructor is a special member function that is called when an object is destroyed. It is used to free up any resources that the object was using, such as memory allocated with the new operator or open files. Destructors are an essential feature of object-oriented programming, as they allow you to manage resources in a controlled way and prevent memory leaks and other errors.

A destructor is declared using the class name preceded by a tilde (~). It does not take any arguments, and its name must match the class name. The destructor is typically declared in the public section of the class, just like the constructor, as it needs to be accessible from outside the class.

Here's an example of a class with a destructor:

class MyFile {
public:
  MyFile(const char* filename) {
    file = fopen(filename, "r");
  }
  ~MyFile() {
    if (file != NULL) {
      fclose(file);
    }
  }
private:
  FILE* file;
};

In this example, the MyFile class has a constructor that takes a C-style string as an argument and opens a file with the specified name for reading. The constructor uses the fopen function to open the file and assigns the resulting file pointer to the file data member.

The MyFile class also has a destructor that closes the file if it is open. The destructor checks whether the file pointer is not null before calling the fclose function to close the file. This ensures that the file is closed even if an exception is thrown or the program terminates unexpectedly.

Destructors can also be used to free up memory allocated with the new operator. For example, if a class has a data member that represents a dynamically allocated array, the destructor can use the delete operator to free up the memory:

class MyArray {
public:
  MyArray(int size) {
    data = new int[size];
    this->size = size;
  }
  ~MyArray() {
    delete[] data;
  }
private:
  int* data;
  int size;
};

In this example, the MyArray class has a constructor that takes an integer as an argument and allocates a dynamic array of that size using the new operator. The constructor assigns the resulting pointer to the data data member and stores the size of the array in the size data member.

The MyArray class also has a destructor that frees up the memory allocated with the new operator using the delete[] operator. This ensures that the memory is released when the object is destroyed and prevents memory leaks.

In addition to freeing up resources, destructors can also be used to perform other cleanup tasks, such as releasing locks or closing network connections. Destructors are called automatically by the C++ runtime when an object is destroyed, which ensures that the cleanup tasks are performed even if an exception is thrown or the program terminates unexpectedly.

In summary, destructors are an essential feature of object-oriented programming in C++. They are used to free up resources that the object was using, such as memory or open files, and perform other cleanup tasks. Destructors are declared using the class name preceded by a tilde (~), and they must match the class name. By understanding how destructors work and their uses, you can create classes that are more robust, maintainable, and reusable.