0

how can I overload double subscript operator [][] in c++ ?

I have tried a number of ways.. No specific answer is available anywhere..

thanks in advance..

I have tried this.. But I know its not correct

class Matrix { int row; int col; int ** values; int ptr; public: Matrix(const int r, const int c) { ptr = -1; row = r; col = c; values = new int*[row]; for(int i=0; i<row; i++) { values[i] = new int[col]; } } int & operator[](int p) { if(ptr == -1) ptr = p; return values[ptr][p]; } }; 
1
  • 2
    There's no thing like a double subscript operator [][]. Commented Dec 31, 2014 at 8:54

1 Answer 1

5

double subscript operator [][]

There isn't a double-subscript operator in C++. What you can do is overload operator[] to return an object that also overloads operator[]. This will enable you to write m[i][j].

Sign up to request clarification or add additional context in comments.

1 Comment

thank u very much.. that was precise and clear..

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.