gpt4 book ai didi

c++ - 类名和同名变量的声明

转载 作者:行者123 更新时间:2023-11-30 05:49:09 26 4
gpt4 key购买 nike

标准 N4296::3.3.1/4 [basic.scope.declarative]:

exactly one declaration shall declare a class name or enumeration name that is not a typedef name and the other declarations shall all refer to the same variable or enumerator, or all refer to functions and function templates; in this case the class name or enumeration name is hidden (3.3.10).

我知道如果在同一个声明区域中有一个具有相同名称的变量/函数声明,规则是关于隐藏类的名称。但是 exactly one 有点令人困惑。以下命名空间完全有效:

namespace A
{
struct A;
struct A { };
int A;
}

DEMO

尽管我们两次声明了struct A(两次声明一个结构,一次声明一个变量)。怎么了?我在规则中失去了什么?

最佳答案

// Exactly one class may have the name:
struct A; // Declaration of a new class.
struct A { }; // Definition, but not a declaration of a new name. Doesn't count.

// Aside from the class, exactly one variable may share the name:
extern int A; // Declaration of a variable.
int A; // Definition of a variable.

关于c++ - 类名和同名变量的声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27895689/

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