I was trying to implement a simple matrix class below is its code. Can any one tell me why the the print statement "CALLED CONST" isn't getting executed ? It was supposed to get printed while executing these two statements .
Can someone help ?Code:int x=M(3,4)+5; cout<<M(3,4)<<endl;
Code:#include <cstdio> #include <vector> #include <iostream> #include <algorithm> using namespace std; template<class T> class Matrix { private: unsigned r,c; vector<T>data; public: Matrix(){}; Matrix(unsigned _r,unsigned _c):r(_r),c(_c) { data.resize(r*c); } T& operator ()(unsigned _r,unsigned _c) { puts("CALLED"); return data[c*_r+_c]; } T operator ()(unsigned _r,unsigned _c) const { puts("CALLED CONST"); return data[c*_r+_c]; } }; int main() { Matrix<int>M(5,5); M(3,4)=19191; int x=M(3,4)+5; cout<<M(3,4)<<endl; return 0; }



LinkBack URL
About LinkBacks


