如何重载运算符 [] 让动态矩阵返回值!?C++

How to overload operator [] for dynamic matrix to return a value!? C++

本文关键字:动态 返回值 C++ 何重载 重载 运算符      更新时间:2023-10-16
class matrix {
public:
  int **A;
  int nrColoane;
  int nrLinii;
 public:        
     int& operator[] (int nrLinii, int nrColoane);
};

怕这是不可能的。解决问题的常用方法是重载运算符 ()。

class Matrix {
    public:
        int& operator () (int i, int j);
};

正如其他人所提到的,使用 operator() 解决问题是很常见的。另一种方法是仍然使用 operator[] 接受一个参数,但返回一个表示行的对象(并保留对原始对象的引用)。然后行对象将再次实现operator[]并返回实际值。