§3.3.7/1 第 5 项:
The potential scope of a declaration that extends to or past the end of a class definition also extends to the regions defined by its member definitions, even if the members are defined lexically outside the class (this includes static data member definitions, nested class definitions, and member function definitions, including the member function body and any portion of the declarator part of such definitions which follows the declarator-id, including a parameter-declaration-clause and any default arguments (8.3.6)).
是否可以在本段给出的第一个示例中识别出这样的声明?
typedef int c;
enum { i = 1 };
class X {
char v[i];
int f() { return sizeof(c); }
char c;
enum { i = 2 };
};
最佳答案
它看起来好像在说,除此之外,除了上面的答案之外,给定类定义之外的所有代码,即使 X::f
在类之外定义,如下所示:
typedef int c;
enum { i = 1 };
class X {
char v[i];
int f();
char c;
enum { i = 2 };
};
int X::f() {
return sizeof(c);
}
即,在 X::f
定义的上下文中, c
会引用成员变量X::c
, 而不是 typedef
上面,因为即使它看起来像是在全局定义的,f
实际住在X
的范围。
关于c++ - §3.3.7/1 第 5 项中突出显示的句子是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31484414/