C++ Destructors
In this page:
What is a Destructor?
A destructor is a special member function that runs automatically when an object goes out of scope or is explicitly deleted. It shares the class's name prefixed with a tilde (~), takes no arguments, and has no return type, and its job is to clean up whatever the object was holding onto.
Example: What is a Destructor?
#include <iostream>
class Box {
public:
~Box() { std::cout << "Box destroyed" << std::endl; }
};
int main() {
Box b;
return 0;
} // destructor runs here
Login to try C/C++/Java/PHP code in the editor
Destructors and Resource Cleanup
Destructors are essential for releasing resources an object owns — most commonly memory allocated with new. If you don't free that memory inside the destructor, it leaks every time an object of that class is destroyed, since nothing else will ever reclaim it.
Example: Destructors and Resource Cleanup
#include <iostream>
class Buffer {
int *data;
public:
Buffer() { data = new int[10]; }
~Buffer() { delete[] data; std::cout << "Memory freed" << std::endl; }
};
int main() {
Buffer b;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Implicit vs Explicit Destructors
If you don't write a destructor yourself, the compiler provides an implicit one automatically. That default is fine for simple classes, but the moment your class manages a raw resource like heap memory or a file handle, you need to write an explicit destructor to release it correctly.
Example: Implicit vs Explicit Destructors
#include <iostream>
class Simple {
int value = 5; // no destructor written: compiler generates an implicit one
};
int main() {
Simple s;
std::cout << "No custom cleanup needed" << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Destructor Order of Execution
When multiple objects are destroyed together, such as local variables going out of scope at the end of a function, they're destroyed in the exact reverse order of their creation — the last object created is always the first one destroyed, mirroring how a stack unwinds.
Example: Destructor Order of Execution
#include <iostream>
class Item {
int id;
public:
Item(int i) : id(i) {}
~Item() { std::cout << "Destroying item " << id << std::endl; }
};
int main() {
Item a(1);
Item b(2);
return 0; // b destroyed first, then a (reverse of creation order)
}
Login to try C/C++/Java/PHP code in the editor
Destructors with Pointers
Objects created on the heap with new are not destroyed automatically when they go out of scope, because a raw pointer going out of scope only destroys the pointer, not what it points to. You must call delete explicitly to trigger the destructor and free that memory.
Example: Destructors with Pointers
#include <iostream>
class Box {
public:
~Box() { std::cout << "Box destroyed" << std::endl; }
};
int main() {
Box *ptr = new Box();
delete ptr; // must delete manually -- a raw pointer going out of scope does NOT call the destructor
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: