A translation unit usually consists of single source file with all #include
d files and results in one object file.
A name in namespace scope has by default external linkage, meaning you can refer that name from other translation units (with scope resolution operator or using directive). But if the name is qualified with static
, the linkage becomes internal, and the name can not be referred outside the translation unit in which it was defined.
In your example you could access a
if the namespace A
, the name a
and main
method is in the same translation unit. But in main
, you are declaring another variable a
, which hides the a
in namespace A
. and the a
in main is not initialized, so when you print, it actually prints garbage value from a
declared in main
. If you want to use a
from A
in main
, use like cout<<A::a
or use using namespace A;
in the source file containing main
.