Destructor (computer programming)In object-oriented programming, a destructor (sometimes abbreviated dtor[1]) is a method which is invoked mechanically just before the memory of the object is released.[2] It can happen either 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. Destructors are necessary in resource acquisition is initialization (RAII). With most kinds of automatic garbage collection algorithms, the releasing of memory may happen a long time after the object becomes unreachable, making destructors unsuitable for time-critical purposes. In these languages, the freeing of resources is done through an lexical construct (such as try-finally, Python's Syntax
In C++The destructor has the same name as the class, but with a tilde (~) before it.[2] For example, a class called foo will have the destructor 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. A destructor should never throw an exception.[8] Non-class scalar types have what's called a pseudo-destructor which can be accessed by using int f() {
int a = 123;
using T = int;
a.~T();
return a; // undefined behavior
}
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.[9] Exampleimport std;
class Foo {
public:
Foo():
data_(new char[sizeof("Hello, World!")]) {
std::strcpy(data_, "Hello, World!");
}
Foo(const Foo& other) = delete; // disable copy construction
Foo& operator=(const Foo& other) = delete; // disable assignment
~Foo(void) { delete[] data_; }
private:
friend std::ostream& operator<<(std::ostream& os, const Foo& foo) {
os << foo.data_;
return os;
}
char* data_;
};
int main() {
Foo foo;
std::cout << foo << std::endl;
}
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."[10]). ![]() In C with GCC extensionsThe GNU Compiler Collection's C compiler comes with 2 extensions that allow implementing destructors:
In Java
Java provides 2 interfaces that implement destructors, public final class Destructors implements AutoCloseable {
@Override
public void close() { }
}
public final class Test {
try (Destructors dtors = new Destructors()) {
...
}
// after try-with-resources, dtors.close() will be called
}
Prior to Java 7, a "try-finally" block was used. public final class Destructors {
public void close() { }
}
public final class Test {
try {
Destructors dtors = new Destructors();
} finally {
dtors.close()
}
}
In XojoDestructors in Xojo (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 with a ~ (tilde) prefix. The newer form uses the name Class Foobar // Old form Sub ~Foobar() End Sub // New form Sub Destructor() End Sub End Class See also
References
|
Portal di Ensiklopedia Dunia