I've received an excercise to implement a generic Matrix class. One of the tasks is as follows: "Overload operator() which gets parameterse (unsigned int, unsigned int) twice, once as const and once as a non-const.".
Now, after much searching, I understood that this should be how their signatures:
T operator()(unsigned int rows, unsigned int colls) const; //cout << "A" << endl;
T & operator()(unsigned int rows, unsigned int colls); //cout << "B" << endl;
however, no matter which way I'm trying to call them only the second one is called:
int main()
{
Matrix<int> mat(2, 3);
mat(1, 1) = 3;
int i= (9 + mat(1, 1)) ;
i = mat(2, 1);
cout << i << endl;
if (i == mat(1, 2)) {}
int j = Matrix<int>(2, 2)(1, 1); //calls constructor and then subscription.
}
The output will be:
B
B
B
0
B
B