[英]In C++, must operator[] () be a member function?
In C++, must operator []()
always be a member function? 在C ++中,
operator []()
必须始终是成员函数吗? If yes, why? 如果是,为什么?
I read "An operator must be a member function" in book 我在书中读到“一个操作员必须是一个成员函数”
"The C++ Programming Language Special Edition" page 287. “C ++编程语言特别版”第287页。
From the C++ draft: 从C ++草案:
13.5.5 Subscripting [over.sub]
13.5.5订阅[over.sub]
operator[] shall be a non-static member function with exactly one parameter.
operator []应该是一个非静态成员函数,只有一个参数。 It implements the subscripting syntax
它实现了下标语法
postfix-expression [ expression ]
后缀表达式[表达式]
Thus, a subscripting expression x[y] is interpreted as x.operator for a class object x of type T if T::operator exists and if the operator is selected as the best match function by the overload resolution mechanism (13.3.3).
因此,如果T :: operator存在,并且如果运算符被重载决策机制选为最佳匹配函数,则下标表达式x [y]被解释为类型为T的类对象x的x.operator(13.3.3) 。
I can't find it explicit in the spec, but I'm assuming the reason is because it's expected to return an lvalue.
我在规范中找不到它,但我假设原因是因为它预计会返回一个左值。
Scratch that: it didn't make sense. 抓一点:它没有意义。 It returns an lvalue of the subscripted type not the object type.
它返回下标类型的左值而不是对象类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.