C++ overload operator [ ][ ]

后端 未结 5 1880
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-28 12:35

I have class CMatrix, where is \"double pointer\" to array of values.

class CMatrix {
public:
    int rows, cols;
    int **arr;
};

I simpl

5条回答
  •  迷失自我
    2020-11-28 12:55

    You can do it by overloading operator[] to return an int*, which is then indexed by the second application of []. Instead of int* you could also return another class representing a row, whose operator[] gives access to individual elements of the row.

    Essentially, subsequent applications of operator[] work on the result of the previous application.

提交回复
热议问题