Search⌘ K

const ref Parameters

Explore how to use const ref parameters in D to maintain immutability when passing arguments to functions. Understand how this applies to structs and classes, enabling safe handling of immutable objects while preventing unauthorized modifications. This lesson also covers the difference between non-const and const member functions.

This lesson is about how parameters and member functions are marked as const so that they can be used with immutable variables as well.

Although the examples in this lesson use only structs, const member functions also apply to classes.

immutable objects

You have already seen that it is not possible to modify immutable variables:

immutable readingTime = TimeOfDay(15, 0);

readingTime cannot be modified:

 ...