FACTOID # 173: More than half of all doctors in Finland are female.
 
 Home   Encyclopedia   Statistics   Countries A-Z   Flags   Maps   Education   Forum   FAQ   About 
 
 
 
WHAT'S NEW
RECENT ARTICLES
More Recent Articles »
 

SEARCH ALL

FACTS & STATISTICS    Advanced view

Search encyclopedia, statistics and forums:

 

 

(* = Graphable)

 

 


Encyclopedia > Destructor (computer science)

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 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. Object-oriented programming (OOP) is a programming paradigm that uses objects to design applications and computer programs. ... Used mainly in object-oriented programming, the term method refers to a piece of code that is exclusively associated either with a class (called class methods, static methods, or factory methods) or with an object (called instance methods). ... In strictly mathematical branches of computer science the term object is used in a purely mathematical sense to refer to any thing. While this interpretation is useful in the discussion of abstract theory, it is not concrete enough to serve as a primitive datatype in the discussion of more concrete... A resource, also referred to as system resource, is any physical or virtual system component of a computer system with limited availability. ... Resource Acquisition Is Initialization, often referred to by the acronym RAII, is a popular programming technique in C++ and D. The technique combines acquisition and release of resources with initialization and uninitialization of variables. ...


In a language with an 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. In computing, garbage collection is a system of automatic memory management which seeks to reclaim memory used by objects which will never be referenced in the future. ... Resource Acquisition Is Initialization, often referred to by the acronym RAII, is a popular programming technique in C++ and D. The technique combines acquisition and release of resources with initialization and uninitialization of variables. ... In object-oriented programming languages that use automatic garbage collection, a finalizer is a special method that is executed when an object is garbage collected. ...

Contents

Virtual destructor

A virtual destructor is a destructor that can be overridden by sub classes. In object-oriented programming (OOP), a virtual function or virtual method is a function whose behavior, by virtue of being declared virtual, is determined by the definition of a function with the same signature furthest in the inheritance lineage of the instantiated object on which it is called. ...


Examples

C++

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 new keyword, then its destructor is called when the pointer that points to the objected is deleted. This particular class holds a_pointer to a list of character strings.

 class myclass { public: char** a_pointer; void newstring(void) { a_pointer[current] = new char[90]; current++; } ~myclass() // THIS IS THE DESTRUCTOR FUNCTION { int n=0; while (n <= current) { delete a_pointer[n++]; } } private: int current; }; 

REALbasic

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 the class easier. REALbasic (RB) is an object-oriented dialect of the BASIC programming language developed and commercially marketed by REAL Software, Inc in Austin, Texas for Mac OS X, Microsoft Windows, and Linux. ... Refactoring is the process of rewriting a computer program or other material to improve its structure or readability, while explicitly keeping its meaning or behavior. ...

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

Test edit


See also

In object-oriented programming languages that use automatic garbage collection, a finalizer is a special method that is executed when an object is garbage collected. ... In object-oriented programming, a constructor (sometimes shorted 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 Java and other object-oriented languages) or dynamically constructed... In computer science, the object lifetime (or life cycle) of an object in object-oriented programming is the time between an objects creation (also known as instantiation or construction) till the object is no longer used, and is destructed or freed. ... Resource Acquisition Is Initialization, often referred to by the acronym RAII, is a popular programming technique in C++ and D. The technique combines acquisition and release of resources with initialization and uninitialization of variables. ...

References

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

  Results from FactBites:
 
Destructor (computer science) - Wikipedia, the free encyclopedia (200 words)
In object-oriented programming, a destructor is a method which is automatically invoked when the object is destroyed.
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.
  More results at FactBites »


 
 

COMMENTARY     


Share your thoughts, questions and commentary here
Your name
Your comments

Want to know more?
Search encyclopedia, statistics and forums:

 


Lesson Plans | Student Area | Student FAQ | Reviews | Press Releases |  Feeds | Contact
The Wikipedia article included on this page is licensed under the GFDL.
Images may be subject to relevant owners' copyright.
All other elements are (c) copyright NationMaster.com 2003-5. All Rights Reserved.
Usage implies agreement with terms, 1022, m