inline T& operator() (int row, int col) { return this->m_data[row*NC + col]; }
returns a non-constant data reference in m_data[row * NC + col] , which means that I can do
auto& ref = obj(x, y); ref = BAD_DATA;
and your object cannot stop me. The const option protects against this and can be fingerprinted as const, because as a result it has no side effects on the object.
inline const T& operator() (int row, int col) const { return this->m_data[row*NC + col]; }
This allows me to extend constness to the outside:
void myFunc(const Object& obj) { const auto& ref = obj(x, y); std::cout << x << ',' << y << " = " << ref << '\n'; }
source share