I have this:
#include <iostream>
using namespace std;
// Variable created inside namespace
namespace first
{
int val = 500;
}
namespace
{
int val = 400;
}
// Global variable
//int val = 100;
int main()
{
// Local variable
int val = 200;
// These variables can be accessed from
// outside the namespace using the scope
// operator ::
cout << first::val << '\n';
cout << ::val << '\n';
cout << val << '\n';
return 0;
}
The ::val
in this case will provide val = 400
. But if I remove the comment to the global variable then the global namespace will be reached by ::val
. So, in that case, how can I access the unnamed namespace?
int val = 400;
This is the output with
// Global variable
int val = 100; //Not commented.
500
100
200
detail
) or just naming your global variables more specifically. – Foehn