C++ multidimensional array operator

后端 未结 7 1314
栀梦
栀梦 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:32

    No, there's just operator[]. As an alternative, you can overload:

    int &operator()(int x, int y);
    

    You can use that:

    m(4, 5);
    
    0 讨论(0)
  • 2021-01-05 16:43

    You need to overload operator[] and make it return a new class which only has another operator[].

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-05 16:54

    Not directly, but you can achieve the same functionality overloading operator[]() and having it return something that supports operator[]() itself.

    For example:

    class A {
      std::vector<std::vector<int> > vec;
    public:
      std::vector<int>& operator[] (int x)
      {
          return vec[x];
      }
    };
    

    would allow you to write:

    A a;
    //...
    int y = a[1][2];
    

    because a[1] returns a std::vector<int> to which you can apply operator[](2).

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-05 16:54

    As mentionned before, there is no such thing as operator[][]. However, here is an an implementation using nested classes similar to what "jalf" proposed. For sake of simplicity, I hardcoded a 3x3 raw array.

        class Array2D final{
        public:
            class PartialArr final{
        private:
            friend Array2D;
            PartialArr(Array2D* ptr, int index) :original(ptr), firstIndex(index) {}
            int firstIndex;
            Array2D* original;
        public:
            int& operator[](int index) { return this->original->data[firstIndex][index];  }
        };
    
            PartialArr operator[](int index) { return PartialArr(this, index); }
        private:
            int data[3][3];
        };
    
    

    This solution prevents the user of Array2D to manipulate the data directly when indexing only the first dimension of the array.

    const versions of both operator[] could also be added to make the class complete.

    0 讨论(0)
提交回复
热议问题