Cod*_*asy 1 c++ scope namespaces
好的,我有这个代码,我知道它不是很好的编程实践.我忘记了它的名字.
int main()
{
int variable;
{
int variable;
}
}
Run Code Online (Sandbox Code Playgroud)
这是一个本地命名空间还是什么?我只是记不起这样做的正确用语.
这{ ... }
是一个块或复合语句,它创建一个嵌套范围.(这不是namespace
.)
内部作用域中与外部作用域中具有相同名称(因此隐藏)声明的声明的这种特殊情况有时称为阴影.
g ++可以对此发出警告.引用手册:
`-Wshadow'
Warn whenever a local variable or type declaration shadows another
variable, parameter, type, or class member (in C++), or whenever a
built-in function is shadowed. Note that in C++, the compiler will
not warn if a local variable shadows a struct/class/enum, but will
warn if it shadows an explicit typedef.
Run Code Online (Sandbox Code Playgroud)
(正如亚当罗森菲尔德在评论中指出,-Wshadow
没有被启用-Wall
,-Wextra
或者-pedantic
,你必须明确地启用它.)