In object-oriented programming , a destructor (sometimes abbreviated dtor ) is a method which is invoked mechanically just before the memory of the object is released. It can happen when its lifetime is bound to scope and the execution leaves the scope, when it is embedded in another object whose lifetime ends, or when it was allocated dynamically and is released explicitly. Its main purpose is to free the resources (memory allocations, open files or sockets, database connections , resource locks , etc.) which were acquired by the object during its life and/or deregister from other entities which may keep references to it. Use of destructors is needed for the process of Resource Acquisition Is Initialization (RAII).
9-534: Destructor may refer to: Destructor (computer programming) , in object-oriented programming, a method which is automatically invoked when an object is destroyed Euronymous (1968–1993), guitarist and co-founder of the Norwegian black metal band Mayhem Spanish warship Destructor (1886) , a fast ocean-going torpedo gunboat Destructor, a Marvel Comics character; see Advanced Idea Mechanics Cherax destructor ,
18-893: 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. 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." ). 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
27-546: 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. Scott Meyers Too Many Requests If you report this error to the Wikimedia System Administrators, please include
36-471: The Dispose pattern . The destructor has the same name as the class, but with a tilde (~) before it. For example, a class called foo will have the destructor ~ foo () . Additionally, destructors have neither parameters nor return types. As stated above, a destructor for an object is called whenever the object's lifetime ends. If the object was created as an automatic variable , its lifetime ends and
45-523: 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. 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
54-444: 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,
63-447: The link to point directly to the intended article. Retrieved from " https://en.wikipedia.org/w/index.php?title=Destructor&oldid=1207038704 " Category : Disambiguation pages Hidden categories: Short description is different from Wikidata All article disambiguation pages All disambiguation pages Destructor (computer programming) With most kinds of automatic garbage collection algorithms,
72-482: The releasing of memory may happen a long time after the object becomes unreachable, making destructors (called finalizers in this case) unsuitable for most purposes. In such languages, the freeing of resources is done either through a lexical construct (such as try..finally, Python's "with" or Java's "try-with-resources"), which is the equivalent to RAII, or explicitly by calling a function (equivalent to explicit deletion); in particular, many object-oriented languages use
81-531: The scientific name for the Common Yabby Destructor, a character in the Futurama episode " Raging Bender " a municipal incinerator ; the term destructor was used well into the 20th century Topics referred to by the same term [REDACTED] This disambiguation page lists articles associated with the title Destructor . If an internal link led you here, you may wish to change
#949050