Operator overloading
The time has come to return to user-defined types and see how to implement operator overloads. It's important to note before we get started that D does not support operator overloads as free functions; they must be part of a class
or struct
declaration. We'll be turning away from templates for part of this discussion; some operator overloads are required to be templates, but others can either be templates or normal member functions. We'll look at the latter group first. For the official documentation on operator overloading, pay a visit to http://dlang.org/operatoroverloading.html.
Non-templated operator overloads
There are a handful of operator overloads that are not required to be templates. These cover the comparison operators, the function call operator, and the assignment operator, as well as the index, index-assign, and dollar operators. We'll visit them each in that order.
Comparison overloads – opEquals and opCmp
The equality operators ==
and !=
are overloaded with...