One of the biggest differences between C++ and C# is in how object lifetimes are managed. I was going to have a general topic on destructors just like constructors but there are just too many differences. So today, we’re going to look at C++ destructors.

A destructor is just a method that belongs to a class or struct that gets called by the compiler at a specific time when an instance of your class goes out of scope. It gives you the ability to clean up or put things back the way you found them. It’s the last method your instance will run.

There are two main uses of destructors in C++, to free memory that was allocated, and to release other resources such as file handles.

You also learn some of the philosophy that guides how C++ decides what order to perform things such as calling destructors. This is important when multiple destructors all need to be called.