gpt4 book ai didi

c++ - [over.unary]/2中注释中提到的隐藏的编译示例

转载 作者:可可西里 更新时间:2023-11-01 16:36:07 29 4
gpt4 key购买 nike

[over.unary]/2

The unary and binary forms of the same operator are considered to have the same name. [ Note: Consequently, a unary operator can hide a binary operator from an enclosing scope, and vice versa. —end note ]

我想看一个发生这种隐藏的片段的编译示例。

最佳答案

一个相当简单的例子1:

struct foo {
void operator+(foo const&) {}
};

struct bar : foo {
void operator+() {}
};

int main() {
bar a, b;
a + b; // Can't add two bars
}

成员函数的名称是operator+,所以在bar中声明的在重载时隐藏在foo中的。这使得 main 末尾的添加不正确。

但如果您有两个 foo 对象(不是 bar),添加就完全没问题了。


<子>1 - 对不起,这是一个非编译的,但通常名称隐藏的问题是它阻止程序突然构建。

关于c++ - [over.unary]/2中注释中提到的隐藏的编译示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49858290/

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