I am quite new to c++ and having trouble with namespaces.
#include <iostream>
int x = 10;
namespace ns {
int x = 5;
namespace ns_nested {
int z = x; //z is assigned a value of 5??
}
}
int main(){
std::cout << ns::ns_nested::z;
}
This prints 5
. Initially, I thought this was because I was just changing the value of x to 5
from 10
.
But if I change the first declaration of x to const int x = 10
, it still prints 5
.
So, my question here is twofold:
I though the variables declared in a global scope was... well... global, as in just one instance of it was available to all. So, why/how am I able to declare an instance of a variable with the same name again?
If I were to assign the value of
z
to the value ofx
that was declared globally instead of the one in the outer namespace, how would I do it?