Operator[][] overload

前端 未结 18 1863
轮回少年
轮回少年 2020-11-22 05:46

Is it possible to overload [] operator twice? To allow, something like this: function[3][3](like in a two dimensional array).

If it is pos

18条回答
  •  灰色年华
    2020-11-22 06:39

    If, instead of saying a[x][y], you would like to say a[{x,y}], you can do like this:

    struct Coordinate {  int x, y; }
    
    class Matrix {
        int** data;
        operator[](Coordinate c) {
            return data[c.y][c.x];
        }
    }
    

提交回复
热议问题