After running the following code segment, the output is
Outer.
Inner.
Inner.
I know this is about the usage of “namespace”, but do not understand why the call of “Inner::message()” print out “Inner”. Thanks for explanation.
#include <iostream>
using namespace std;
namespace Outer
{
void message( );
namespace Inner
{
void message( );
}
}
int main( )
{
Outer::message( );
Outer::Inner::message( );
using namespace Outer;
Inner::message( );
return 0;
}
namespace Outer
{
void message( )
{
cout<< "Outer.\n";
}
namespace Inner
{
void message( )
{
cout << "Inner.\n";
}
}
}
This makes perfect sense. Your are using namespace
Outer. Inside of namespaceOuteryou have two members…void message();void Inner::message();You explicitly scoped into
Innerand called message there. Why would you expect otherwise? Had you not explicitly scoped intoInner, then it would have calledvoid Outer::message();.