I tried the following piece of code. When i compile, i get error that there there are ambiguous instances of first_var, whereas i have introduced using namespace second_space before the last cout
I guess this is because the last cout is using both the namespaces. There is no override concept for namespaces ? Is there anyway a namespace scope can be ended or it continues from the using namespace point to the end of file?
#include<iostream.h>
namespace first_space{
int first_var;
}
namespace second_space{
int first_var = 1;
}
int main()
{
cout<<"Hello World"<<endl;
cout<<"First Namespace Variable using namespace identifier:"<<first_space::first_var<<endl;
using namespace first_space;
cout<<"First Namespace Variable using using identifier:"<<first_var<<endl;
using namespace second_space;
cout<<"Second Namespace Variable using using identifier:"<<first_var<<endl;
}
Edit 1:
I tried something like this below. Declared a variable with same name inside the main, assigned a value 1 to it and then used using namespace below that. But I see that, the value of first_var is printed as 1 in the last two cout. There is no ambiguity here. So the namespace didn't have any effect? Why is it so?
#include<iostream.h>
namespace first_space{
int first_var;
}
namespace second_space{
int first_var = 1;
}
int main()
{
int first_var =1 ;
using namespace first_space;
cout<<"Hello World"<<endl;
cout<<"First Namespace Variable using namespace identifier:"<<first_space::first_var<<endl;
cout<<"First Namespace Variable using using identifier:"<<first_var<<endl;
// using namespace second_space;
cout<<"Second Namespace Variable using using identifier:"<<first_var<<endl;
}
Output:
Hello World
First Namespace Variable using namespace identifier:0
First Namespace Variable using using identifier:1
Second Namespace Variable using using identifier:1
stop using namespaceconcept, as far as I'm aware...using namespacedirective is used in header files, which should be avoided.