矩阵中的运算符重载:如何同时重载 () 和 =

operator overloading in matrices: how to overload () and = at the same time

我正在尝试创建一个 class 矩阵并尝试像这样在该索引处分配值

M(0,3)=3;

通过运算符重载。 无法弄清楚如何重载此运算符 它的原型应该是这样的吗?

void operator()=(int i,int j,int s);//how to overload?

float operator()(int i,int j);//this can be used to get that number form that index.

没有operator()=。您希望 operator() 到 return 对矩阵中元素的引用。然后就可以给引用的元素赋值了。

float& operator()(int i,int j);
const float& operator()(int i, int j) const;

您可能还需要一个 const 重载,return 是一个 const 引用。