Search⌘ K

Definition, Construction, and Destruction

Explore how classes are defined and constructed in D, including the use of constructors and the unique behavior of destructors. Understand key rules about class destruction related to garbage collection to ensure proper memory management and avoid undefined behavior in your programs.

We'll cover the following...

Definition

Classes are defined by the class keyword instead of the struct keyword:

class ChessPiece {
    // ...
}

Construction

As with structs, the name of the constructor is this. Unlike structs, class objects cannot be constructed by the { } syntax.

D
class ChessPiece {
dchar shape;
this(dchar shape) {
this.shape = shape;
}
}
void main() {
}

Unlike structs, there is no automatic object construction where the constructor parameters are ...