const and inout Member Functions
Explore how const member functions guarantee no modification of objects, allowing calls on immutable instances. Understand inout member functions' role in transferring mutability from objects to return types, ensuring type safety in D. This lesson helps you effectively manage object mutability using const and inout to write clearer and more robust D code.
We'll cover the following...
We'll cover the following...
const member functions
Some member functions do not make any modifications to the object that they are called on. An example of such a function is toString():
struct TimeOfDay {
// ...
string toString() {
return format("%02s:%02s", hour, minute);
}
// ...
}
Since the whole purpose of toString() is to represent the object in a string format anyway, it should not modify the object.
A member function that does not modify the object is declared by the ...