解决了不同文件中 定义相同名字的不同函数之类的问题?
在一个命名空间中定义的全局标识符 其作用域为该命名空间
当在一个命名空间外部需要使用该命名空间中定义的全局标识符时 需要使用该命名空间的名字和域解析符来修饰
main.cpp
好像函数的声明之类的 也要放在命名空间里
#include <iostream>
namespace A{
extern int s;//这里的extern我还是不太懂 但是如果不加的话 linker报错 重复命名
void test();
}
void test(){
std::cout<<"test function in main.cpp\n";
}
int s=5;
int main(){
A::test();
test();
std::cout<<"s in test.app="<<A::s<<std::endl;
std::cout<<"s in main.app="<<s<<std::endl;
}
test.cpp
#include <iostream>
namespace A{
int s=0;
void test(){
std::cout<<"test function in test.cpp\n";
}
}