假设这样一种情况,当一个班上有两个名叫 Zara 的学生时,为了明确区分它们,我们在使用名字之外,不得不使用一些额外的信息,比如他们的家庭住址,或者他们父母的名字等等。 同样的情况也出现在 C++ 应用程序中。例如,您可能会写一个名为 xyz() 的函数,在另一个可用的库中也存在一个相同的函数 xyz()。这样,编译器就无法判断您所使用的是哪一个 xyz() 函数。 因此,引入了命名空间这个概念,专门用于解决上面的问题,它可作为附加信息来区分不同库中相同名称的函数、类、变量等。使用了命名空间即定义了上下文。本质上,命名空间就是定义了一个范围。 我们举一个计算机系统中的例子,一个文件夹(目录)中可以包含多个文件夹,每个文件夹中不能有相同的文件名,但不同文件夹中的文件可以重名。

image.png

定义命名空间

命名空间的定义使用关键字namespace,后跟命名空间的名称,如下所示:

  1. namespace namespace_name {
  2. // 代码声明
  3. }

为了调用带有命名空间的函数或变量,需要在前面加上命名空间的名称,如下所示:

  1. name::code; // code 可以是变量或函数

让我们来看看命名空间如何为变量或函数等实体定义范围:

  1. #include <iostream>
  2. using namespace std;
  3. // 第一个命名空间
  4. namespace first_space
  5. {
  6. void func()
  7. {
  8. cout << "Inside first_space" << endl;
  9. }
  10. }
  11. // 第二个命名空间
  12. namespace second_space
  13. {
  14. void func()
  15. {
  16. cout << "Inside second_space" << endl;
  17. }
  18. } // namespace second_space
  19. int main()
  20. {
  21. first_space::func(); // 调用第一个命名空间中的函数
  22. second_space::func(); // 调用第二个命名空间中的函数
  23. return 0;
  24. }
  25. /*
  26. Inside first_space
  27. Inside second_space
  28. */

using 指令

您可以使用using namespace指令,这样在使用命名空间时就可以不用在前面加上命名空间的名称。这个指令会告诉编译器,后续的代码将使用指定的命名空间中的名称。

  1. #include <iostream>
  2. using namespace std;
  3. // 第一个命名空间
  4. namespace first_space
  5. {
  6. void func()
  7. {
  8. cout << "Inside first_space" << endl;
  9. }
  10. }
  11. // 第二个命名空间
  12. namespace second_space
  13. {
  14. void func()
  15. {
  16. cout << "Inside second_space" << endl;
  17. }
  18. }
  19. // using first_space::func;
  20. using namespace first_space;
  21. int main()
  22. {
  23. // 调用第一个命名空间中的函数
  24. func();
  25. second_space::func();
  26. return 0;
  27. }
  28. /*
  29. Inside first_space
  30. Inside second_space
  31. */

using 指令也可以用来指定命名空间中的特定项目。例如,如果您只打算使用 std 命名空间中的 cout 部分,您可以使用如下的语句:

  1. using std::cout;

随后的代码中,在使用 cout 时就可以不用加上命名空间名称作为前缀,但是std命名空间中的其他项目仍然需要加上命名空间名称作为前缀,如下所示:

  1. #include <iostream>
  2. using std::cout;
  3. int main ()
  4. {
  5. cout << "std::endl is used with std!" << std::endl;
  6. return 0;
  7. }

using指令引入的名称遵循正常的范围规则。名称从使用using指令开始是可见的,直到该范围结束。此时,在范围以外定义的同名实体是隐藏的。

不连续的命名空间

命名空间可以定义在几个不同的部分中,因此命名空间是由几个单独定义的部分组成的。一个命名空间的各个组成部分可以分散在多个文件中
所以,如果命名空间中的某个组成部分需要请求定义在另一个文件中的名称,则仍然需要声明该名称。下面的命名空间定义可以是定义一个新的命名空间,也可以是为已有的命名空间增加新的元素:

  1. namespace namespace_name {
  2. // 代码声明
  3. }

嵌套的命名空间

命名空间可以嵌套,您可以在一个命名空间中定义另一个命名空间,如下所示:

  1. namespace namespace_name1 {
  2. // 代码声明
  3. namespace namespace_name2 {
  4. // 代码声明
  5. }
  6. }

您可以通过使用 :: 运算符来访问嵌套的命名空间中的成员:

  1. // 访问 namespace_name2 中的成员
  2. using namespace namespace_name1::namespace_name2;
  3. // 访问 namespace:name1 中的成员
  4. using namespace namespace_name1;

在上面的语句中,如果使用的是 namespace_name1,那么在该范围内 namespace_name2 中的元素也是可用的,如下所示:

  1. #include <iostream>
  2. using namespace std;
  3. // 第一个命名空间
  4. namespace first_space{
  5. void func(){
  6. cout << "Inside first_space" << endl;
  7. }
  8. // 第二个命名空间
  9. namespace second_space{
  10. void func(){
  11. cout << "Inside second_space" << endl;
  12. }
  13. }
  14. }
  15. using namespace first_space::second_space;
  16. int main ()
  17. {
  18. // 调用第二个命名空间中的函数
  19. func();
  20. return 0;
  21. }
  22. /*
  23. Inside second_space
  24. */

命名空间内变量和函数及全局变量的作用域

  1. #include <iostream>
  2. using namespace std;
  3. int a = 200; //定义一个全局变量
  4. namespace A
  5. {
  6. int a = 100;
  7. namespace B //嵌套一个命名空间B
  8. {
  9. int a =20;
  10. }
  11. }
  12. int main(int argc, char *argv[])
  13. {
  14. cout <<"A::a ="<< A::a << endl;
  15. cout <<"A::B::a ="<<A::B::a << endl;
  16. cout <<"a ="<<a << endl;
  17. cout <<"::a ="<<::a << endl;
  18. int a = 30;
  19. cout <<"a ="<<a << endl;
  20. cout <<"::a ="<<::a << endl;
  21. return 0;
  22. }
  23. /*
  24. A::a =100
  25. A::B::a =20
  26. a =200
  27. ::a =200
  28. a =30
  29. ::a =200
  30. */

⚠️注意: 全局变量 a 表达为 ::a,用于当有同名的局部变量时来区别两者

补充关于 using 的错误事例:

  1. #include <iostream>
  2. using namespace std;
  3. namespace A
  4. {
  5. int a = 100;
  6. int fun()
  7. {
  8. cout << "a = " << a << endl;
  9. }
  10. namespace B //嵌套一个命名空间B
  11. {
  12. int a = 20;
  13. int fun()
  14. {
  15. cout << "a = " << a << endl;
  16. }
  17. }
  18. }
  19. int main(int argc, char *argv[])
  20. {
  21. cout << a << endl;
  22. fun();
  23. return 0;
  24. }

这样会出错:会显示 a 变量和 fun 函数 “was not declared in this scope”,即找不到这个 a 和 fun 函数。
解决办法: 用 using 来告诉编译器用到的是哪个命名空间内的内容。在 main() 上面加 using namespace A; 或者 using namespace A::B; 。这样就可以使用其中的 a 和 fun()。但是不能同时使用,因为这样也会导致编译出错,编译器器不知道要去使用哪个 a 和 fun()。