Operator[][] overload

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

    #include
    
    using namespace std;
    
    class Array 
    {
         private: int *p;
         public:
              int length;
              Array(int size = 0): length(size)
              {
                    p=new int(length);
              }
              int& operator [](const int k)
              {
                   return p[k];
              }
    };
    class Matrix
    {
          private: Array *p;
          public: 
                int r,c;
                Matrix(int i=0, int j=0):r(i), c(j)
                {
                     p= new Array[r];
                }
                Array& operator [](const int& i)
                {
                     return p[i];
                }
    };
    
    /*Driver program*/
    int main()
    {
        Matrix M1(3,3); /*for checking purpose*/
        M1[2][2]=5;
    }
    

提交回复
热议问题