Who*_*ian 11 c++ namespaces global local object
#include <iostream>
#include <string>
using namespace std;
string a;
namespace myNamespace
{
string a;
void output()
{
cout << a << endl;
}
}
int main()
{
a = "Namespaces, meh.";
myNamespace::a = "Namespaces are great!";
myNamespace::output();
}
Run Code Online (Sandbox Code Playgroud)
结果是"命名空间很棒!".那么有没有办法访问命名空间myNamespace内部的全局字符串而不仅仅是本地字符串?
Ton*_*ion 16
像这样:
void output()
{
cout << ::a << endl; //using :: = the global namespace
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
5205 次 |
最近记录: |