C++ 中重载双下标运算符 [][]

Mit*_*kar 0 c++ double overloading operator-keyword

如何在 C++ 中重载双下标运算符 [][] ?

我尝试了很多方法..没有任何具体的答案..

提前致谢..

我已经尝试过这个..但我知道它不正确

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

        }

};
Run Code Online (Sandbox Code Playgroud)

NPE*_*NPE 5

双下标运算符[][]

C++ 中没有双下标运算符。您可以做的是重载operator[]以返回一个也重载的对象operator[]。这将使您能够编写m[i][j].