The destructor has the same name as the class, but with a tilde (~) before it.9 For example, a class called foo will have the destructor ~foo(). Additionally, destructors have neither parameters nor return types.10 As stated above, a destructor for an object is called whenever the object's lifetime ends.11 If the object was created as an automatic variable, its lifetime ends and the destructor is called automatically when the object goes out of scope. Because C++ does not have garbage collection, if the object was created with a new statement (dynamically on the heap), then its destructor is called when the delete operator is applied to a pointer to the object. Usually that operation occurs within another destructor, typically the destructor of a smart pointer object.
In inheritance hierarchies, the declaration of a virtual destructor in the base class ensures that the destructors of derived classes are invoked properly when an object is deleted through a pointer-to-base-class. Objects that may be deleted in this way need to inherit a virtual destructor.
A destructor should never throw an exception.12
Non-class scalar types have what's called a pseudo-destructor which can be accessed by using typedef or template arguments. This construct makes it possible to write code without having to know if a destructor exists for a given type.
In older versions of the standard, pseudo-destructors were specified to have no effect, however that was changed in a defect report to make them end the lifetime of the object they are called on.13
Objects which cannot be safely copied and/or assigned should be disabled from such semantics by declaring their corresponding functions as deleted within a public encapsulation level. A detailed description of this method can be found in Scott Meyers' popular book, Effective Modern C++ (Item 11: "Prefer deleted functions to private undefined ones."14).
The GNU Compiler Collection's C compiler comes with 2 extensions that allow implementing destructors:
Destructors in Xojo (REALbasic) can be in one of two forms. Each form uses a regular method declaration with a special name (with no parameters and no return value). The older form uses the same name as the Class with a ~ (tilde) prefix. The newer form uses the name Destructor. The newer form is preferred because it makes refactoring the class easier.
"dtor". TheFreeDictionary.com. Retrieved 2018-10-14. https://acronyms.thefreedictionary.com/dtor ↩
Sebesta, Robert W. (2012). ""11.4.2.3 Constructors and Destructors"". Concepts of Programming Languages (print) (10th ed.). Boston, MA, USA: Addison-Wesley. p. 487. ISBN 978-0-13-139531-2. 978-0-13-139531-2 ↩
"Finalizers (C# Programming Guide)". https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/finalizers ↩
Constructors and Destructors, from PHP online documentation http://www.php.net/manual/en/language.oop5.decon.php ↩
"3. Data model — Python 2.7.18 documentation". https://docs.python.org/2/reference/datamodel.html#object.__del__ ↩
"3. Data model — Python 3.10.4 documentation". https://docs.python.org/3/reference/datamodel.html#object.__del__ ↩
"Destructors - the Rust Reference". https://doc.rust-lang.org/stable/reference/destructors.html ↩
GotW #47: Uncaught exceptions Accessed 31 July 2011. http://www.gotw.ca/gotw/047.htm ↩
Smith, Richard; Voutilainen, Ville. "P0593R6:Implicit creation of objects for low-level object manipulation". open-std.org. Retrieved 2022-11-25. https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/p0593r6.html ↩
Scott Meyers: Effective Modern C++, O'REILLY, ISBN 9781491903995 /wiki/ISBN_(identifier) ↩
C "destructor" function attribute https://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html#Function-Attributes ↩
Erickson, Jon (2008). Hacking the art of exploitation. No Starch Press. ISBN 978-1-59327-144-2. 978-1-59327-144-2 ↩