gpt4 book ai didi

c++ - 矩阵中的运算符重载 : how to overload () and = at the same time

转载 作者:行者123 更新时间:2023-11-30 03:54:12 25 4
gpt4 key购买 nike

我正在尝试制作一类矩阵并尝试像这样在该索引处分配值

M(0,3)=3;

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

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() 返回对矩阵中元素的引用。然后你可以分配给引用的元素。

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

您可能还需要一个返回 const 引用的 const 重载。

关于c++ - 矩阵中的运算符重载 : how to overload () and = at the same time,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29728460/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com