gpt4 book ai didi

c++ - 用于取值和赋值的矩阵下标运算符

转载 作者:行者123 更新时间:2023-11-30 05:43:45 24 4
gpt4 key购买 nike

我有一个 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/

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