C++ multidimensional array operator

后端 未结 7 1368
栀梦
栀梦 2021-01-05 16:22

it is possible to overload somehow operator for multidimensional array?

Something like:

class A {
  ...
  int& operator[][] (const int x, const i         


        
7条回答
  •  [愿得一人]
    2021-01-05 16:48

    Nope, that is not possible. There are two alternatives, though:

    You can have operator[] return an array of a smaller dimension (For a 3D array, it will return a 2D array, for a 2D array it will return a 1D array, and for a 1D array, it will return a single element). Then you can "string them together" with the syntax you want. (arr[x][y][z])

    Alternatively, you can overload operator(), because that can take multiple arguments.

    Then you can use it like this, to index into a 3D array for example: arr(x,y,z)

    But you can't overload [][] or [][][] as a single operator.

提交回复
热议问题