C++ namespace的作用

namespace:命名空间或者叫名字空间,传统的c++只有一个全局的namespace,但是由于现在的程序规模越来越大,程序的分工越来越细,全局作用域就变得越来越拥挤,每个人都可能使用相同的名字来实现不同的库,于是程序员在合并程序的时候就可能出现名字的冲突。namespace引入了复杂性,解决了这个问题。namespace允许像类、对象、函数聚集在一个名字下,本质上讲namespace是对全局作用域的细分。

比如这样的程序大家经常见到的:

1 #include <iostream>
2 using namespace std;
3 
4 int main()
5 {
6     printf("hello world !");
7     return 0;
8 }

大部分可能对namespace了解也就这么多。但是namespace远不止如此,下面让我再多了解一下namespace。

namespace的基本格式是:

 namespace identifier
{
    entities;
}

举个例子

namespace exp
{
    int a,b;
}
有点类似于类,但完全是两种不同的类型。
为了在namespace外使用namespace内的变量我们使用::操作符,如下
exp::a
exp::b

使用namespace可以有效的避免重定义的问题。

如下面的例子:

 1 #include <iostream>
 2 using namespace std;
 3 
 4 namespace first
 5 {
 6   int var = 5;
 7 }
 8 
 9 namespace second
10 {
11   double var = 3.1416;
12 }
13 
14 int main () {
15   cout << first::var << endl;
16   cout << second::var << endl;
17   return 0;
18 }

输出的结果为:
5
3.1416
两个全局变量都是名字都是var,但是他们不在同一个namespace中所以没有冲突。

 

关键字using可以帮助从namespace中引入名字到当前的声明区域

 1 #include <iostream>
 2 using namespace std;
 3 
 4 namespace first
 5 {
 6   int x = 5;
 7   int y = 10;
 8 }
 9 
10 namespace second
11 {
12   double x = 3.1416;
13   double y = 2.7183;
14 }
15 
16 int main () {
17   using first::x;
18   using second::y;
19   cout << x << endl;
20   cout << y << endl;
21   cout << first::y << endl;
22   cout << second::x << endl;
23   return 0;
24 }

输出是
5
2.7183
10
3.1416
就如我们所指定的第一个x是first::x,y是second.y

 

using也可以导入整个的namespace

 1 #include <iostream>
 2 using namespace std;
 3 
 4 namespace first
 5 {
 6   int x = 5;
 7   int y = 10;
 8 }
 9 
10 namespace second
11 {
12   double x = 3.1416;
13   double y = 2.7183;
14 }
15 
16 int main () {
17   using namespace first;
18   cout << x << endl;
19   cout << y << endl;
20   cout << second::x << endl;
21   cout << second::y << endl;
22   return 0;
23 }

输出是
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指令局限在很小的作用域中,这样他们的效用就会受到限制并且易于使用。类似的例子有

 1 #include <iostream>
 2 using namespace std;
 3 
 4 namespace first
 5 {
 6   int x = 5;
 7 }
 8 
 9 namespace second
10 {
11   double x = 3.1416;
12 }
13 
14 int main () {
15   {
16     using namespace first;
17     cout << x << endl;
18   }
19   {
20     using namespace second;
21     cout << x << endl;
22   }
23   return 0;
24 }

输出是
5
3.1416
可以看到两个不同的namespace都被限制在了不同作用域中了,他们之间就没有冲突。

 

namespace也支持嵌套

 1 #include <iostream>
 2 
 3 namespace first 
 4 {
 5     int a=10;
 6     int b=20;
 7 
 8     namespace second
 9     {   
10         double a=1.02;
11         double b=5.002;
12         void hello();
13     }   
14 
15     void second::hello()
16     {   
17     std::cout <<"hello world"<<std::endl;
18     }
19 }
20 
21 int main()
22 {
23     using namespace first;
24 
25     std::cout<<second::a<<std::endl;
26     second::hello();
27 }

输出是
1.02
hello world
在namespace first中嵌套了namespace second,seond并不能直接使用,需要first来间接的使用。

 

posted @ 2017-03-15 10:35  evilsnake  阅读(936)  评论(0编辑  收藏  举报