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
  • 13
  • 116
  • 190

1 Answers1

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].

NPE
  • 486,780
  • 108
  • 951
  • 1,012