Destructor in C++ Explained with Examples (Complete Guide)
In C++, objects often acquire resources like memory, file handles, or network connections during their lifetime. If these resources are not released properly, it can lead to memory leaks and inefficient programs.
What is destructor in c++
A destructor in C++ is a special member function that is automatically called when an object goes out of scope or when a dynamically allocated object is explicitly deleted.
The main purpose of a destructor is to release the resources acquired by the object, such as dynamically allocated memory, open files, or network connections.
Syntax of destructor in C++
Destructor has the same name as the class , preceded by a tilde symbol (~)
Destructors do not take parameters and do not return any value.
A destructor cannot be overloaded, meaning a class can have only one destructor.
In C++, destructors are automatically called in the following situations:
Local (Automatic) Objects Go Out of Scope
cpp
void function() { MyClass obj; // Constructor called // Do something} // Destructor called here - obj goes out of scope
Program Termination
cpp
MyClass globalObj; // Global objectint main() { static MyClass staticObj; // Static object return 0; // Both destructors called after main ends}
For Dynamically Allocated Objects (when using delete)
cpp
MyClass* ptr = new MyClass(); // Constructor calleddelete ptr; // Destructor explicitly called
Types of Destructors in C++
In most cases, destructors in C++ fall into two categories:
1. Default Destructor
A destructor automatically generated by the compiler when no user-defined destructor is provided.
2. User-defined Destructor
A destructor written by the programmer to release resources such as dynamically allocated memory or file handles.
Advantages of Destructors in C++
there are various advantages of using destructor some of them are :
1. Prevention of Memory Leaks
Destructor automatically deallocate the dynamically allocated memory
2. Automatic Resource Management (RAII)
Destructors enable the RAII (Resource Acquisition Is Initialization) paradigm, automatically releasing resources when objects die.
3. Exception Safety
Destructors are called during stack unwinding, ensuring cleanup even when exceptions are thrown
4. Cleanup in Reverse Order
Ensures dependent resources are released in the correct order.
Constructor vs Destructor in C++
Feature
Constructor
Destructor
Purpose
Initialize objects, allocate resources
Clean up, release resources
Syntax
Same name as the class
Same name as the class but with a (~) symbol
Called When
Object creation
Object destruction / out of scope
Parameters
Can have parameters
No parameters
Number per class
Can be multiple constructors per class
Only one destructor per class
Key takeaways
A destructor in C++ is a special member function that is automatically called when an object is destroyed.
It is mainly used to release resources such as dynamically allocated memory, files, or connections.
The destructor has the same name as the class but starts with a tilde (~) symbol.
A class can have only one destructor, and it cannot be overloaded.
Destructors are called when:
an object goes out of scope
a program terminates
a dynamically allocated object is deleted using delete
Destructors play an important role in RAII (Resource Acquisition Is Initialization) and help prevent memory leaks.
Conclusion
Destructors are an essential part of C++ object lifecycle management. They ensure that resources acquired by objects are properly released when those objects are destroyed. By automatically cleaning up memory and other resources, destructors help prevent memory leaks and make programs safer and more efficient.
FAQ
1. Can a destructor be overloaded in C++?
No, a class can have only one destructor, so destructor overloading is not allowed.
2. Can a destructor have parameters?
No. Destructors cannot accept parameters and do not return any value.
3. When is a destructor called in C++?
A destructor is called automatically when an object goes out of scope, when the program terminates, or when a dynamically allocated object is deleted.
4. Can a destructor be virtual in C++?
Yes. A destructor can be declared as virtual. Virtual destructors are commonly used in inheritance to ensure that the correct destructor is called when deleting objects through base class pointers.