Two square bracket overloading

fafa picture fafa · Apr 12, 2011 · Viewed 20.2k times · Source

I am writing a matrix class in c++ and trying to overload some operator like = and >> and << etc.

I was unable to overload operator [][] for matrix class. if i have an object of class matrix like M1 then i can use this way for giving value to each element:

M1[1][2]=5;

OR

int X;

X=M1[4][5];

Answer

Alexander Gessler picture Alexander Gessler · Apr 12, 2011

Just overload operator[] and make it return a pointer to the respective row or column of the matrix. Since pointers support subscripting by [], access by the 'double-square' notation [][] is possible then.

You can also overload operator() with two arguments.