I have a generic Matrix
class, and I have two version of operator()
,
a const method that returns a const reference to the index,
and a non-const method that returns a non const reference to the index (which allows me to change it.
I tried to use the non-const version by using const_cast and calling for the const version, but for some reason it doesn't work:
template<typename T>
class Matrix
{
std::vector<T> _matrix;
unsigned int _rows;
unsigned int _cols;
public:
const T& operator()(unsigned int row, unsigned int col) const
{
return _matrix[col + (_cols * row)];
}
T& operator()(unsigned int row, unsigned int col)
{
return const_cast<T&>(static_cast<const Matrix*> (*this).operator(row, col));
}
};
It doesn't allow me to add the (row, col) to the operator in the last line. Any ideas?