Operator[][] overload

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

    The shortest and easiest solution:

    class Matrix
    {
    public:
      float m_matrix[4][4];
    
    // for statements like matrix[0][0] = 1;
      float* operator [] (int index) 
      {
        return m_matrix[index];
      }
    
    // for statements like matrix[0][0] = otherMatrix[0][0];
      const float* operator [] (int index) const 
      {
        return m_matrix[index];
      }
    
    };
    

提交回复
热议问题