Destructor

Learn about destructors in D with the help of an example.

Destructor definition

The destructor includes the operations that must be executed when the lifetime of an object ends.

The compiler-generated, automatic destructor executes the destructors of all of the members in order. For that reason, as it is with the constructor, there is no need to define a destructor for most structs.

However, sometimes some special operations may need to be executed when an object’s lifetime ends. For example, an operating system resource that the object owns may need to be returned to the system, a member function of another object may need to be called, a server running somewhere on the network may need to be notified that a connection to it is about to be terminated, etc.

svg viewer

Create a free account to view this lesson.

By signing up, you agree to Educative's Terms of Service and Privacy Policy