问题描述
extern int iNSIntl; //defined in other cpp
static int iNSIntl=2;
namespace{
int iNSIntl=0;
}
void main ( int argc, char** argv )
{
cout<<::iNSIntl<<endl;
}
在main()中,::: iNSIntl引用静态的,我如何引用外部的或在命名空间中呢?
In the main(),::iNSIntl refer to static one,how Can I refer to the extern one or within the namespace?
推荐答案
namespace
{
void Foo()
{
}
}
void Foo()
{
}
现在,如果您调用Foo()
,编译器将会很困惑.它们在Foo
的相等可调用范围内.您将需要显式调用::Foo
,后者将调用全局方法(第二个方法).您现在不能在这里呼叫匿名Foo
.现在,如果您注释掉全局Foo
,则对Foo
的调用(无::那里)将起作用,因为编译器在首次查找全局版本之后将在匿名名称空间中进行查找.
Now if you call Foo()
, the compiler will be confused. They are both under equal callable scope for Foo
. You will need to explicitly call ::Foo
which calls the global method (the 2nd one). You cannot call the anonymous Foo
here now. Now if you comment out the global Foo
, then your call to Foo
(no :: there) will work since the compiler will look in the anonymous namespace after it first looks for a global version.
Does that answer your question?
这篇关于“静态变量"和“静态变量"之间有什么区别并在“匿名命名空间"中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!