A more fundamental question is whether to define the
operator() (by the class) for this purpose or not. Alas, this decision has already been made by the author(s) of the matrix class, so we should consider its usage to be compatible with the intent of the author(s).
Indeed, this is what most matrix (or multi-dimensional array) libraries would choose to implement in C++.
Note that your first version of code may not be future-proof. There is no guarantee that
Matrix.matrix exists in future versions of the library, or that
Matrix matrix; mat.matrix[k][j] continues to be a valid expression due to future library changes.
Possibilities why such future breaking changes might happen:
- The matrix library used to have compile-time constant (template-parameterized) matrix sizes, but was later upgraded to support run-time matrix sizes.
- This means internally it is no longer declaring the
T matrix[M][N]; anymore.
- The matrix library used to maintain an array of pointers to the start of each row, but then decided to eliminate that in order to save a little bit of memory.
- This means internally it is no longer declaring
T** matrix;, therefore the field may have been removed, or that you cannot use
matrix[row] to get the starting pointer to a particular row.
- The matrix library may have implemented type erasure, therefore the class no longer knows the type of
T at compile-time; instead, it only contains an untyped pointer to the start of the allocated memory block, as in
void* matrix; or
uchar* matrix;, where
uchar has no relationship to the numeric type of the matrix.
However, if the matrix library is under your full control, that is: you wrote it, nobody else can change it, and you have sole discretion on what future changes to be made to it, then the "future-proof" issue does not apply to you.