Destructors in C++ are responsible for cleaning up when an object is destroyed. This makes them ideal for managing resources that need to be acquired during an object's lifetime and released when the object is no longer needed.
For example, consider a class that manages a dynamically allocated buffer:
#include <cstddef>
class DynamicBuffer {
public:
DynamicBuffer(std::size_t size)
: data_{new int[size]}, size_{size} {}
~DynamicBuffer() {
delete[] data_;
}
private:
int* data_;
std::size_t size_;
};
int main() {
{
DynamicBuffer buf(1024);
// Usee buf...
}
// buf is destroyed here, calling destructor
}
Here, the constructor allocates memory for the buffer, and the destructor ensures that this memory is freed when the DynamicBuffer
object goes out of scope.
This technique is known as Resource Acquisition Is Initialization (RAII). The idea is that a resource (like memory, a file handle, a network connection, etc.) should be acquired in the constructor (during initialization) and released in the destructor. This way, we can't forget to release the resource, and we don't have to manually manage the object's lifetime.
Some key points about destructors:
delete
 was called on a pointer to the object.So use destructors to clean up any resources that your object owns. This is a key part of writing safe, leak-free C++ code.
Answers to questions are automatically generated and may not have been reviewed.
A crash tour on how we can create custom types in C++ using classes, structs and enums