namespace的意义
namespace中文意思是命名空间或者叫名字空间,传统的C++只有一个全局的namespace,但是由于现在的程序的规模越来越大,程序的分工越来越细,全局作用域变得越来越拥挤,每个人都可能使用相同的名字来实现不同的库,于是程序员在合并程序的时候就会可能出现名字的冲突。namespace引入了复杂性,解决了这个问题。namespace允许像类,对象,函数聚集在一个名字下。本质上讲namespace是对全局作用域的细分。
namespace的使用
创建namespace的基本格式是
namespace identifier
{
entities;
}
举个例子,
使用namespace可以有效的避免重定义的问题
#include <iostream>
using namespace std;
namespace first
{
int var = 5;
}
namespace second
{
double var = 3.1416;
}
int main () {
cout << first::var << endl;
cout << second::var << endl;
return 0;
}结果是
5
3.1416
两个全局变量都是名字都是var,但是他们不在同一个namespace中所以没有冲突。
namespace支持嵌套
#include <iostream>namespace first
{
int a=10;
int b=20;
namespace second
{
double a=1.02;
double b=5.002;
void hello();
}
void second::hello()
{
std::cout <<"hello world"<<std::endl;
}
}
int main()
{
using namespace first;
std::cout<<second::a<<std::endl;
second::hello();
}
输出是
1.02
hello world
在namespace first中嵌套了namespace second,seond并不能直接使用,需要first来间接的使用。
namespace可以使用别名,在对一些名字比较长的namespace使用别名的话,是一件很惬意的事。但是与using相同,最好避免在头文件使用namespace的别名(f比first更容易产生冲突)。
namespace f = first;
未命名的namespace定义,提供了单独的作用域,它类似于静态全局声明的使用(从声明到文件尾,其他文件不可以using引用,因为没有名称):
namespace { int count = 0;} //这里的count是唯一的
//在程序的其它部分中count是有效的
void chg_cnt (int i) { count = i; }
using的使用
使用某个变量
using name::var;//
#include <iostream>
using namespace std;
namespace first
{
int x = 5;
int y = 10;
}
namespace second
{
double x = 3.1416;
double y = 2.7183;
}
int main () {
using first::x;
using second::y;
cout << x << endl;
cout << y << endl;
cout << first::y << endl;
cout << second::x << endl;
return 0;
}使用整 个namespace
using namesapce name;//
#include <iostream>
using namespace std;
namespace first
{
int x = 5;
int y = 10;
}
namespace second
{
double x = 3.1416;
double y = 2.7183;
}
int main () {
using namespace first;
cout << x << endl;
cout << y << endl;
cout << second::x << endl;
cout << second::y << endl;
return 0;
}这里我们不能在“using namespace first;“下加一句“using namespace second;“,为什么呢?
这样做无异于直接完全的忽视namespace first和namespace second,会出现重复定义的结果
在头文件中,我们通常坚持使用显式的限定,并且仅将using指令局限在很小的作用域中,这样他们的效用就会受到限制并且易于使用。类似的例子有
#include <iostream>
using namespace std;
namespace first
{
int x = 5;
}
namespace second
{
double x = 3.1416;
}
int main () {
{
using namespace first;
cout << x << endl;
}
{
using namespace second;
cout << x << endl;
}
return 0;
}
3869

被折叠的 条评论
为什么被折叠?



