C++ multidimensional array operator

后端 未结 7 1371
栀梦
栀梦 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:54

    There's no operator like that. I implemented, some times ago, a matrix trying to be close to the stl standards. And I used this method: first I've overloaded the operator[] to return another class that I called _C_row:

    _C_row operator[](size_type index) { return _C_row(/*some parameters*/); } ///< This operator is overloaded to permit the use of double pointer notation.
    _C_row operator[](size_type index) const { return _C_row(/*some parameters*/); } ///< This operator is overloaded to permit the use of double pointer notation.
    

    And in _C_row I overloaded more than the operator[]:

    value_type operator*() { return _r[0]; }
    pointer operator->() { return _i[_idx]; }
    double_pointer operator&() { return &(_i[_idx]); }
    reference operator[](size_type col) { return _r[col]; }
    const_reference operator[](size_type col) const { return _r[col]; }
    

    I found this solution is very flexible. I hope my answer could be useful for you.

提交回复
热议问题