Operator[][] overload

前端 未结 18 1854
轮回少年
轮回少年 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:24

    Using C++11 and the Standard Library you can make a very nice two-dimensional array in a single line of code:

    std::array<std::array<int, columnCount>, rowCount> myMatrix {0};
    
    std::array<std::array<std::string, columnCount>, rowCount> myStringMatrix;
    
    std::array<std::array<Widget, columnCount>, rowCount> myWidgetMatrix;
    

    By deciding the inner matrix represents rows, you access the matrix with an myMatrix[y][x] syntax:

    myMatrix[0][0] = 1;
    myMatrix[0][3] = 2;
    myMatrix[3][4] = 3;
    
    std::cout << myMatrix[3][4]; // outputs 3
    
    myStringMatrix[2][4] = "foo";
    myWidgetMatrix[1][5].doTheStuff();
    

    And you can use ranged-for for output:

    for (const auto &row : myMatrix) {
      for (const auto &elem : row) {
        std::cout << elem << " ";
      }
      std::cout << std::endl;
    }
    

    (Deciding the inner array represents columns would allow for an foo[x][y] syntax but you'd need to use clumsier for(;;) loops to display output.)

    0 讨论(0)
  • 2020-11-22 06:27

    One approach is using std::pair<int,int>:

    class Array2D
    {
        int** m_p2dArray;
    public:
        int operator[](const std::pair<int,int>& Index)
        {
           return m_p2dArray[Index.first][Index.second];
        }
    };
    
    int main()
    {
        Array2D theArray;
        pair<int, int> theIndex(2,3);
        int nValue;
        nValue = theArray[theIndex];
    }
    

    Of course, you may typedef the pair<int,int>

    0 讨论(0)
  • 2020-11-22 06:29

    Sample code:

    template<class T>
    class Array2D
    {
    public:
        Array2D(int a, int b)  
        {
            num1 = (T**)new int [a*sizeof(int*)];
            for(int i = 0; i < a; i++)
                num1[i] = new int [b*sizeof(int)];
    
            for (int i = 0; i < a; i++) {
                for (int j = 0; j < b; j++) {
                    num1[i][j] = i*j;
                }
            }
        }
        class Array1D
        {
        public:
            Array1D(int* a):temp(a) {}
            T& operator[](int a)
            {
                return temp[a];
            }
            T* temp;
        };
    
        T** num1;
        Array1D operator[] (int a)
        {
            return Array1D(num1[a]);
        }
    };
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        Array2D<int> arr(20, 30);
    
        std::cout << arr[2][3];
        getchar();
        return 0;
    }
    
    0 讨论(0)
  • 2020-11-22 06:30
    struct test
    {
        using array_reference = int(&)[32][32];
    
        array_reference operator [] (std::size_t index)
        {
            return m_data[index];
        }
    
    private:
    
        int m_data[32][32][32];
    };
    

    Found my own simple solution to this.

    0 讨论(0)
  • 2020-11-22 06:30

    My 5 cents.

    I intuitively knew I need to do lots of boilerplate code.

    This is why, instead of operator [], I did overloaded operator (int, int). Then in final result, instead of m[1][2], I did m(1,2)

    I know it is DIFFERENT thing, but is still very intuitive and looks like mathematical script.

    0 讨论(0)
  • 2020-11-22 06:38

    You can use a proxy object, something like this:

    #include <iostream>
    
    struct Object
    {
        struct Proxy
        {
            Object *mObj;
            int mI;
    
            Proxy(Object *obj, int i)
            : mObj(obj), mI(i)
            {
            }
    
            int operator[](int j)
            {
                return mI * j;
            }
        };
    
        Proxy operator[](int i)
        {
            return Proxy(this, i);
        }
    };
    
    int main()
    {
        Object o;
        std::cout << o[2][3] << std::endl;
    }
    
    0 讨论(0)
提交回复
热议问题