我有一个 bool 值矩阵
class BoolMatix
{
};
我想实现 2 个可以做到这一点的下标运算符
BoolMatix b(...);
b[5]=true;
和
bool val=GetTruth(5);
GetTruth(5)
将返回 b[5]
我试过了
const bool operator[](int index);
但这似乎只适用于 GetTruth(index n)
,赋值如何?
最佳答案
你必须返回一个引用
bool& BoolMatrix::operator [](int index);
const bool operator[](int index);
你用它返回一个 const bool,所以你不能给它赋值
关于c++ - 用于取值和赋值的矩阵下标运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30096035/