轉自:http://blog.csdn.net/yao_zhuang/article/details/1853625
?
namespace中文意思是命名空間或者叫名字空間,傳統的C++只有一個全局的namespace,但是由于現在的程序的規模越來越大,程序的分工越來越細,全局作用域變得越來越擁擠,每個人都可能使用相同的名字來實現不同的庫,于是程序員在合并程序的時候就會可能出現名字的沖突。namespace引入了復雜性,解決了這個問題。namespace允許像類,對象,函數聚集在一個名字下。本質上講namespace是對全局作用域的細分。
我想大家都見過這樣的程序吧:
hello_world.c
#include <iostream>
using namespace std;
int main()
{
??? printf("hello world !");
??? return 0;
}
我想很多人對namespace的了解也就這么多了
但是namespace遠不止如此,讓我們再多了解一下namespace
namespace的格式基本格式是
namespace identifier
{
??? entities;
}
舉個例子,
namespace exp
{
??? int a,b;
}
有點類似于類,但完全是兩種不同的類型。
為了在namespace外使用namespace內的變量我們使用::操作符,如下
exp::a
exp::b
使用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中所以沒有沖突。
關鍵字using可以幫助從namespace中引入名字到當前的聲明區域
#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;
}
輸出是
5
2.7183
10
3.1416
就如我們所指定的第一個x是first::x,y是second.y
using也可以導入整個的namespace
#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;
}
輸出是
5
10
3.1416
2.7183
正如我們所預見的導入的整個的first的namespace,前一對x,y的值就是first中的x,y的值。
這里我們不能在“using namespace first;“下加一句“using namespace second;“,為什么呢?
這樣做無異于直接完全的忽視namespace first和namespace second,會出現重復定義的結果,所以前面的hello_world.c中的using指令的使用一定程度上存在問題的,只是因為我們就用了一個namspace,一旦引入了新的namespace這種做法很可能會出現重復定義的問題。
在頭文件中,我們通常堅持使用顯式的限定,并且僅將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;
}
輸出是
5
3.1416
可以看到兩個不同的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提供了單獨的作用域,它類似于靜態全局聲明的使用,可以使用未命名的namespace定義來實現:
namespace { int count = 0;}???????? //這里的count是唯一的
??????????????????????????????????????????????? //在程序的其它部分中count是有效的
void chg_cnt (int i) { count = i; }
參考資料:
《C++必知必會》 [美]Stephen C.Dewhurst 著,榮耀譯?? 條款23名字空間
http://www.cplusplus.com/doc/tutorial/namespaces.html
《C++精粹》???? [美]Ira Pohl 著 王樹武 陳朔鷹 等譯 P15