Suppose I have the following defined in a header file:
namespace MyNamespace
{
Class global_c;
}
Then I do this in a source file:
namespace MyNamespace
{
void MyClass::Function( )
{
::global_c.DoSomething( );
}
}
global_c turns out as undefined by the compiler, if I do just global_c.DoSomething( ); however it compiles fine, if I add ‘using namespace MyNamespace;’ to the top of the file it also works fine.
Since global_c lives in the same namespace as ‘MyClass’ why can’t it be resolved just because ‘::’ is added to the front of it?
Because you are explicitly telling the compiler to use the global namespace by prepending the variable with
::. Asglobal_cdoes not exist in the global namespace it throws an error.The compiler is just doing what you told it to do. Think of
::asGlobal::.