Академический Документы
Профессиональный Документы
Культура Документы
In a language with an automatic garbage collection mechanism, it would be difficult to deterministically ensure
the invocation of a destructor, and hence these languages are generally considered unsuitable for RAII. In such
languages, unlinking an object from existing resources must be done by an explicit call of an appropriate
function (usually called Dispose()). This method is also recommended for freeing resources rather than
using finalizers for that.
Contents
[hide]
1 Destructor syntax
2 In C++
o 2.1 Example
4 REALbasic
5 See also
6 References
[edit]Destructor syntax
C++ has the naming convention in which destructors have the same name as the class of which they are
associated with, but prefixed with a tilde (~).
In Object Pascal, destructors have the keyword "destructor" and can have user-defined names (but
are mostly called "Destroy").
In Moose object system for Perl, the destructor method is named DEMOLISH.
In Objective-C, the destructor method is named "dealloc".
In PHP 5, the destructor method is named "__destruct". There were no destructors in previous
versions of PHP.[1]
[edit]In C++
The destructor has the same name as the class, but with a tilde (~) in front of it. If the object was created as
an automatic variable, its destructor is automatically called when it goes out of scope. If the object was created
with a newexpression, 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.
[edit]Example
#include <cstring>
#include <iostream>
class foo_t
{
friend std::ostream & operator<<( std::ostream & os, foo_t const & foo )
{
os << data;
return os;
}
private:
char * data;
foo_t( foo_t const & other );
public:
foo_t( void ) : data( new char[ 14 ] ) { std::strcpy( data,
"Hello, World!" ); }
~foo_t( void ) { delete [] data; }
};
The GNU Compiler Collection's C compiler comes with 2 extensions that allow to implement destructors:
the "destructor" function attribute allows defining global prioritized destructor functions:
when main() returns, these functions are called in priority order before the process terminates. See also [4]
the "cleanup" variable attribute allows attaching a destructor function to a variable: the function is called
when the variable goes out of scope.
[edit]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.
Class Foobar
// Old form
Sub ~Foobar()
End Sub
// New form
Sub Destructor()
End Sub
End Class
#include <cstring>
#include <iostream>
class foo_t
{
friend std::ostream & operator<<( std::ostream & os, foo_t const & foo )
{
os << data;
return os;
}
private:
char * data;
foo_t( foo_t const & other );
public:
foo_t( void ) : data( new char[ 14 ] ) { std::strcpy( data, "Hello,
World!" ); }
~foo_t( void ) { delete [] data; }
};