我有以下代码,我不知道如何在此设置中访问匿名命名空间内的x。请告诉我怎么做?
#include <iostream>
int x = 10;
namespace
{
int x = 20;
}
int main(int x, char* y[])
{
{
int x = 30; // most recently defined
std::cout << x << std::endl; // 30, local
std::cout << ::x << std::endl; // 10, global
// how can I access the x inside the anonymous namespace?
}
return 0;
}
答案 0 :(得分:1)
答案 1 :(得分:0)
您必须从匿名相同范围内的函数访问它:
#include <iostream>
int x = 10;
namespace
{
int x = 20;
int X() { return x; }
}
int main(int x, char* y[])
{
{
int x = 30; // most recently defined
std::cout << x << std::endl; // 30, local
std::cout << ::x << std::endl; // 10, global
std::cout << X() << std::endl; // 20, anonymous
// how can I access the x inside the anonymous namespace?
}
return 0;
}