Multi-Dimensional Operator Overloading Example

Look at a multi-dimensional operator overloading example.

We'll cover the following

Matrix example

The following Matrix example demonstrates how these operators can be overloaded for a two-dimensional type.

Note that this code can be implemented in more efficient ways. For example, instead of constructing a single-element sub-matrix even when operating on a single element, e.g., by m[i, j], it could apply the operation directly on that element.

Additionally, the writeln(__FUNCTION__) expressions inside the functions have nothing to do with the behavior of the code. They merely help expose the functions that get called behind the scenes for different operator usages.

Also, note that the correctness of dimension values are enforced by template constraints.

Get hands-on with 1400+ tech skills courses.