我在代码中看到了这两种方法。您能解释一下两者之间有什么区别吗?我认为它与 C++ 完成名称空间查找的方式有关,您能否也提供一些有关此的信息,或者可能是一个好的文档的链接?谢谢。
Example:
#include <cstdio>
namespace x {
const int i = 1;
}
namespace y {
namespace x {
const int i = 2;
}
void func()
{
std::printf("x::i = %d\n", x::i);
std::printf("::x::i = %d\n", ::x::i);
}
}
int main()
{
y::func();
return 0;
}
Output:
x::i = 2
::x::i = 1
解释:
所以使用::
开头允许您拼写全局事物的全名。这还允许您区分局部变量和全局变量。
示例2:
#include <cstdio>
const int x = 5;
int main()
{
const int x = 7;
std::printf("x = %d\n", x);
std::printf("::x = %d\n", ::x);
return 0;
}
Output:
x = 7
::x = 5
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)