I’m writing a matrix 3×3 class in c++.
glm::mat3 provides access to matrix data through the [][] operator syntax.
e.g. myMatrix[0][0] = 1.0f; would set the first row, first column entry to 1.0f.
I’d like to provide similar access. How can I overload the [][] operators?
I’ve tried the following, but I get errors:
operator name must be declared as a function
const real operator[][](int row, int col) const
{
// should really throw an exception for out of bounds indices
return ((row >= 0 && row <= 2) && (col >= 0 && col <= 2)) ? _data[row][col] : 0.0f;
}
What’s the correct way to overload this operator?
There is no operator
[][], so you need to overload the[]operator twice: once on the matrix, returning a surrogate object for the row, and once for the returned surrogate row: