今天看到O(1)哥在看C++的課件,里面有一段代碼是解釋命名空間namespace的,PPT上面的代碼編譯不過,
由于第一次接觸C++的namespace,不明白其中的奧妙,于是google之,看了網(wǎng)上的解析之后明白了許多,回過頭來看程序,
才發(fā)現(xiàn)原來是類里面的函數(shù)f()沒有具體實(shí)現(xiàn),怪不得不能定位。具體代碼如下:
#include <stdio.h>
namespace NameSpace1
{
class MyClass
{
public:
void f()
{
m = 1;
printf("%d\n", m);
}
private:
int m;
};
}
namespace NameSpace2
{
class MyClass
{
public:
void f()
{
m = 2;
printf("%d\n", m);
}
private:
int m;
};
}
int main()
{
NameSpace1::MyClass x;
x.f();
NameSpace2::MyClass y;
y.f();
return 0;
}
下面給出的namespace的用法(轉(zhuǎn)載的,由于網(wǎng)絡(luò)上轉(zhuǎn)載太多,不知道誰是原作者了):
Namespaces
名字空間允許像類,對(duì)象和函數(shù)一樣的一組實(shí)體歸屬于一個(gè)名稱。把一個(gè)全局的范圍化分成許多子的范圍,每一個(gè)小的范圍都有它自己的名字。
名字空間的格式是:
namespace identifier{
entities
}
包含在名字空間下的任何類,對(duì)象和函數(shù)中設(shè)立的實(shí)體必須是有效的標(biāo)簽符,例如:
namespace myNamespace{
int a, b;
}
這種情況,在被調(diào)用的名字空間myNamespace內(nèi)部變量a和變量b被定義成了一個(gè)普通的變量,為了從myNamespace外部訪問這些變量,我們必須使用作用域操作符::,例如,從myNamespace外部訪問前面定義的變量,我們要這樣寫:
general::a
general::b
名字空間的功能一個(gè)全局的對(duì)象或者函數(shù)像另外一個(gè)對(duì)象一樣使用了同一個(gè)標(biāo)識(shí)符時(shí)特別有用,會(huì)引起重定義的錯(cuò)誤,例如:
//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;
}
程序運(yùn)行如下:
5
3.1416
這種情況我們使用了兩個(gè)相同名字的全局變量:var。一個(gè)是被定義在first名字空間里的,另一個(gè)是被定義在second名字空間內(nèi)的。因?yàn)橛辛嗣挚臻g所以沒有發(fā)生重定義的錯(cuò)誤。
using
使用using 這個(gè)關(guān)鍵字是為了將一個(gè)來到名字空間內(nèi)的名字介紹到當(dāng)前聲明的區(qū)域,例如:
//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;
}
程序運(yùn)行如下:
5
2.7183
10
3.1416
如何注意這段代碼,x(沒有任何名字空間標(biāo)識(shí)符)是引用first::x,而y引用second::y,正是由于我們?cè)谇懊嬷付?/font>using這個(gè)聲明,所以們?nèi)匀灰褂脴?biāo)識(shí)符全名來訪問first::y 和 second::x。
關(guān)鍵字using也能直接指向一個(gè)實(shí)體名字空間。
//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;
}
程序運(yùn)行如下:
5
10
3.1416
2.7183
這個(gè)情況,我們定義過using namespace frist,所有直接使用x和y沒有使用名字空間標(biāo)識(shí)符而是引用了定義的名字空間frist。
using和using namespace 僅僅是在同一個(gè)塊里有效,它們規(guī)定在整個(gè)代碼內(nèi),是否是在全局范圍里直接使用,例如,如果我們打算首先使用一個(gè)名字空間的對(duì)象,再使用別外一個(gè)名字空間的對(duì)象,我們應(yīng)該這樣做:
//namespace
#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;
}
程序運(yùn)行如下:
5
3.1416
名字空間別名(Namespace alias)
我們能改變一個(gè)已經(jīng)存在的名字空間的命名,相應(yīng)的格式如下:
namespace new_name = current_name;
名字空間std (namespace std)
C++標(biāo)準(zhǔn)類庫中的所有實(shí)體都定義在名字空間std中,這就是為什么我們通常需要包含一個(gè)using namespace std,描述在所有的程序中使用的實(shí)體都定義在iostream中。