Luc*_*ore 20 c++ operator-overloading
是否有适用于一元和运营商的特殊规则?
例如,代码:
#include <iostream>
struct X
{
X() {}
void* operator &() { return NULL; }
};
int main()
{
const X x;
std::cout << &x << std::endl;
X y;
std::cout << &y;
}
Run Code Online (Sandbox Code Playgroud)
产生输出
0xbfbccb33
0
Run Code Online (Sandbox Code Playgroud)
我知道这会像这样编译和运行,因为之前我曾在这里进行过讨论,但我不知道这一点,我原本预计这会无法编译,因为operator &没有声明const.
因此,operator &() const无论是否operator &()过载,编译器似乎都会生成.很好,这很有意义,尤其是样本和输出.
问题是标准中详细说明了这种行为在哪里?
我不是在寻找重复我在问题中已经说明的答案的答案,所以请不要解释我的重载操作符是如何在一个const对象上调用的,因为我已经知道了.