article

In object-oriented programming, a destructor 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. A destructor that returns a value can be used to obtain a representation of an object of a class.

In a language with a automatic garbage collection mechanism, 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.

REALbasic

Example
Destructors in 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 itself with a ~ (tilde) prefix. The newer form uses the name "Destructor." The newer form is the preferred one because it makes refactoring your class easier.

Class Foobar // Old form Sub ~Foobar() End Sub // New form Sub Destructor() End Sub End Class

See also


References


  • Bjarne Stroustrup: The C++ Programming Language, Addison-Wesley, ISBN 0-201-70073-5

Object-oriented programming

Destruktorius (programavimas) | Destruktor | Деструктор (программирование)

 

This article is licensed under the GNU Free Documentation License. It uses material from the "Destructor (computer science)".

Home Pageartsbusinesscomputersgameshealthhospitalshomekids & teensnewsphysiciansrecreationreferenceregionalscienceshoppingsocietysportsworld