c++ - 如何重载数组索引运算符的多个下标?

标签 c++ class operator-overloading

假设我有一个类,就这样foo 。它包含对象 someclass obj[10][10] .

现在我想引用bar ,这是 foo 类型的对象,这样 bar[7]相当于 bar.obj[7]并返回 someclass* 类型的对象。这很简单。

但我还想引用bar以这样的方式 bar[7][3]相当于 bar.obj[7][3]并返回 someclass 类型的对象。现在没那么简单...

怎么做?

最佳答案

in such a way that bar[7] is equivalent to bar.obj[7] and returns an object of type someclass*.

但是如果它返回 someclass*,那么它就不会等同于 bar.obj[7]。它需要返回 someclass(&)[10] 才能等效。

请注意,您可能还需要 const 重载。

But I also want to refer to bar in such a way that bar[7][3]

只要按照描述重载下标运算符,您就可以做到这一点 - 它既适用于您的指针建议,也适用于我的引用建议。

关于c++ - 如何重载数组索引运算符的多个下标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61103141/

相关文章:

c++ - Visual C++ 中的 CreateWindow() 始终返回 null

c++ - LINK2019 中对运算符>> 的 undefined reference 结果

css - 向没有类或id的div添加属性值?

java - Java : new Stream<Integer>(){ . .. } 中的语法是什么意思?

c++ - 重载 += 运算符

c++ - 确定 operator[] 用途的目的

c# - 从 C++ 返回到 C# 层时的位图可视化问题

c++ - 如何在 Linux 下为 C/C++ 中的计时器设置亲和性?

c++ - 我可以克隆 C++ 对象的字节,覆盖原始字节,然后将这些字节复制回来吗?

c++ - 没有对 'pthread_create' 的匹配函数调用