gpt4 book ai didi

c++ - 对运营商的澄清

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:25:36 25 4
gpt4 key购买 nike

我一直在阅读类运算符并遇到以下示例:

class Array {
public:
int& operator[] (unsigned i) { if (i > 99) error(); return data[i]; }
private:
int data[100];
};

这是否意味着我可以用我想要的任何东西替换 []?例如,我可以使用括号吗?

另外,int& operator[] 中的 & 有什么意义?


稍微不那么重要的一点是,使用 int& operator[] 而不是 int& operator[] 在语法上是否正确?

最佳答案

Does this mean I can replace [ and ] with whatever I want?

不!不是所有运营商can be overloaded .

For example, could I use parentheses?

你可以,但你不应该。

Also, what is the significance of the ampersand in int& operator[]?

这意味着您要返回一个 reference到一个 int 变量。

EDIT: On a slightly less important note, would it be syntactically correct to use int& operator [] instead of int& operator[]?

两者没有区别

关于c++ - 对运营商的澄清,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4290148/

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