- Destructor (computer science)
object-oriented programming, a destructor (sometimes shortened to dtor) is a method which is automatically invoked when the object is destroyed. Its main purpose is to clean up and to free the resources which were acquired by the object along its life cycle and unlink it from other objects or resources invalidating any references in the process. The use of destructors is key to the concept of RAII.
In a language with an
automatic garbage collectionmechanism, it is impossible to deterministically ensure the invocation of a destructor, and hence these languages are unsuitable for RAII. In such languages, unlinking an object from existing resources should be done by an explicit call of appropriate function (usually called
Dispose). This method is also recommended for freeing resources rather than using
Finalizers for that.
A virtual destructor is a destructor that can be overridden by subclasses.In C++, use of virtual destructors in inheritance hierarchies facilitates proper clean-up of objects, preventing resource leaks and heap corruption.
In C++, the destructor function is the same name as the class, but with a tilde (~) in front of it. If the object was created locally, its destructor is automatically called, and if the object was created with the
newkeyword, then its destructor is called when the pointer that points to the object is
delete'd. This particular class holds a_pointer to a list of character strings.A destructor is required when dynamically created data elements were used, files were opened, locks have to be unlocked or a copy constructor was used.
REALbasiccan 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 itself with a ~ (tilde) prefix. The newer form uses the name "Destructor." The newer form is the preferred one because it makes refactoringthe class easier.
Class Foobar // Old form Sub ~Foobar() End Sub // New form Sub Destructor() End Sub End Class
Resource Acquisition Is Initialization
Bjarne Stroustrup: "The C++ Programming Language", Addison-Wesley, ISBN 0-201-70073-5
Wikimedia Foundation. 2010.
Look at other dictionaries:
Destructor (computer programming) — In object oriented programming, a destructor (sometimes shortened to dtor) is a method which is automatically invoked when the object is destroyed. Its main purpose is to clean up and to free the resources (which includes closing database… … Wikipedia
Union (computer science) — In computer science, a union is a data structure that stores one of several types of data at a single location. There are only two safe ways of accessing a union object. One is to always read the field of a union most recently assigned; tagged… … Wikipedia
Swap (computer science) — For other uses of swap , see swap (disambiguation). In computer programming, the act of swapping two variables refers to mutually exchanging the values of the variables. Usually, this is done with the data in memory. For example, in a program,… … Wikipedia
Class (computer science) — In object oriented programming, a class is a programming language construct that is used as a blueprint to create objects. This blueprint includes attributes and methods that the created objects all share.More technically, a class is a cohesive… … Wikipedia
Method (computer science) — In object oriented programming, the term method refers to a subroutine that is exclusively associated either with a class (called class methods, static methods, or factory methods) or with an object (called instance methods). Like a procedure in… … Wikipedia
Constructor (computer science) — In object oriented programming, a constructor (sometimes shortened to ctor) in a class is a special block of statements called when an object is created, either when it is declared (statically constructed on the stack, possible in C++ but not in… … Wikipedia
Destructor — may refer to: Destructor (computer science), 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… … Wikipedia
Method (computer programming) — In object oriented programming, a method is a subroutine (or procedure or function) associated with a class. Methods define the behavior to be exhibited by instances of the associated class at program run time. Methods have the special property… … Wikipedia
C Sharp syntax — The correct title of this article is C# syntax. The substitution or omission of the # sign is because of technical restrictions. Main article: C Sharp (programming language) This article describes the syntax of the C# programming language. The… … Wikipedia
Constructor (object-oriented programming) — Programming language comparisons General comparison Basic syntax Basic instructions Arrays Associative arrays String operations … Wikipedia