经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C++ » 查看文章
示例详解C++语言中的命名空间 (namespace)
来源:jb51  时间:2021/8/5 13:21:04  对本文有异议

前言

命名空间可作为附加信息来区分不同库中相同名称的函数、类、变量等。命名空间即定义了上下文,命名空间就是定义了一个范围。

一个文件夹 (目录) 中可以包含多个文件夹,每个文件夹中不能有相同的文件名,但不同文件夹中的文件可以重名。

1. 命名空间

命名空间的定义使用关键字 namespace,后跟命名空间的名称。

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

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

  1. namespace_name::yongqiang; // yongqiang 可以是变量或函数
  1. //============================================================================
  2. // Name : Yongqiang Cheng
  3. // Author : Yongqiang Cheng
  4. // Version : Version 1.0.0
  5. // Copyright : Copyright (c) 2020 Yongqiang Cheng
  6. // Description : Hello World in C++, Ansi-style
  7. //============================================================================
  8.  
  9. #include <iostream>
  10.  
  11. // 第一个命名空间
  12. namespace first_space {
  13. void yongqiang() {
  14. std::cout << "Inside first_space" << std::endl;
  15. }
  16. }
  17.  
  18. // 第二个命名空间
  19. namespace second_space {
  20. void yongqiang() {
  21. std::cout << "Inside second_space" << std::endl;
  22. }
  23. }
  24.  
  25. int main()
  26. {
  27. // 调用第一个命名空间中的函数
  28. first_space::yongqiang();
  29.  
  30. // 调用第二个命名空间中的函数
  31. second_space::yongqiang();
  32.  
  33. return 0;
  34. }
  35.  

Inside first_space
Inside second_space
请按任意键继续. . .

2. using 指令

using namespace 指令会告诉编译器,后续的代码将使用指定的命名空间中的名称。

  1. //============================================================================
  2. // Name : Yongqiang Cheng
  3. // Author : Yongqiang Cheng
  4. // Version : Version 1.0.0
  5. // Copyright : Copyright (c) 2020 Yongqiang Cheng
  6. // Description : Hello World in C++, Ansi-style
  7. //============================================================================
  8.  
  9. #include <iostream>
  10.  
  11. using namespace std;
  12.  
  13. // 第一个命名空间
  14. namespace first_space {
  15. void yongqiang() {
  16. cout << "Inside first_space" << endl;
  17. }
  18. }
  19.  
  20. // 第二个命名空间
  21. namespace second_space {
  22. void yongqiang() {
  23. cout << "Inside second_space" << endl;
  24. }
  25. }
  26.  
  27. using namespace first_space;
  28.  
  29. int main()
  30. {
  31. // 调用第一个命名空间中的函数
  32. first_space::yongqiang();
  33.  
  34. // 调用第二个命名空间中的函数
  35. second_space::yongqiang();
  36.  
  37. // 调用第一个命名空间中的函数
  38. yongqiang();
  39.  
  40. return 0;
  41. }
  42.  

Inside first_space
Inside second_space
Inside first_space
请按任意键继续. . .

using 指令用来指定命名空间中的特定项目,

  1. using std::cout;
  2.  
  3.  

使用 std 命名空间中的 cout 部分,在使用 cout 时就可以不用加上命名空间名称作为前缀,但是 std 命名空间中的其他项目仍然需要加上命名空间名称作为前缀。

  1. //============================================================================
  2. // Name : Yongqiang Cheng
  3. // Author : Yongqiang Cheng
  4. // Version : Version 1.0.0
  5. // Copyright : Copyright (c) 2020 Yongqiang Cheng
  6. // Description : Hello World in C++, Ansi-style
  7. //============================================================================
  8.  
  9. #include <iostream>
  10.  
  11. using std::cout;
  12.  
  13. int main()
  14. {
  15. cout << "yongqiang cheng!" << std::endl;
  16.  
  17. return 0;
  18. }

yongqiang cheng!
请按任意键继续. . .

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

3. 不连续的命名空间

命名空间可以定义在几个不同的部分中,因此命名空间是由几个单独定义的部分组成的。一个命名空间的各个组成部分可以分散在多个文件中。

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

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

4. 嵌套的命名空间

命名空间可以嵌套,您可以在一个命名空间中定义另一个命名空间。

  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. //============================================================================
  2. // Name : Yongqiang Cheng
  3. // Author : Yongqiang Cheng
  4. // Version : Version 1.0.0
  5. // Copyright : Copyright (c) 2020 Yongqiang Cheng
  6. // Description : Hello World in C++, Ansi-style
  7. //============================================================================
  8.  
  9. #include <iostream>
  10.  
  11. using namespace std;
  12.  
  13. // 第一个命名空间
  14. namespace first_space {
  15. void yongqiang() {
  16. cout << "Inside first_space" << endl;
  17. }
  18.  
  19. // 第二个命名空间
  20. namespace second_space {
  21. void yongqiang() {
  22. cout << "Inside second_space" << endl;
  23. }
  24. }
  25. }
  26.  
  27. using namespace first_space::second_space;
  28.  
  29. int main()
  30. {
  31. // 调用第一个命名空间中的函数
  32. first_space::yongqiang();
  33.  
  34. // 调用第二个命名空间中的函数
  35. first_space::second_space::yongqiang();
  36.  
  37. // 调用第二个命名空间中的函数
  38. yongqiang();
  39.  
  40. return 0;
  41. }
  42.  

Inside first_space
Inside second_space
Inside second_space
请按任意键继续. . .

5. 命名空间内变量、函数、全局变量的作用域

  1. //============================================================================
  2. // Name : Yongqiang Cheng
  3. // Author : Yongqiang Cheng
  4. // Version : Version 1.0.0
  5. // Copyright : Copyright (c) 2020 Yongqiang Cheng
  6. // Description : Hello World in C++, Ansi-style
  7. //============================================================================
  8.  
  9. #include <iostream>
  10.  
  11. // 第一个命名空间
  12. namespace first_space {
  13. int num = 99;
  14.  
  15. // 第二个命名空间
  16. namespace second_space {
  17. int num = 11;
  18. }
  19. }
  20.  
  21. // 全局变量
  22. int num = 55;
  23.  
  24. int main()
  25. {
  26. std::cout << "first_space::num = " << first_space::num << std::endl;
  27. std::cout << "first_space::second_space::num = " << first_space::second_space::num << std::endl;
  28. std::cout << "num = " << num << std::endl;
  29. std::cout << "::num = " << ::num << std::endl;
  30.  
  31. int num = 77;
  32. std::cout << "num = " << num << std::endl;
  33. std::cout << "::num = " << ::num << std::endl;
  34.  
  35. return 0;
  36. }
  37.  

全局变量 num 表达为 ::num,当有同名的局部变量时来区别两者。

first_space::num = 99
first_space::second_space::num = 11
num = 55
::num = 55
num = 77
::num = 55
请按任意键继续. . .

  1. //============================================================================
  2. // Name : Yongqiang Cheng
  3. // Author : Yongqiang Cheng
  4. // Version : Version 1.0.0
  5. // Copyright : Copyright (c) 2020 Yongqiang Cheng
  6. // Description : Hello World in C++, Ansi-style
  7. //============================================================================
  8.  
  9. #include <iostream>
  10.  
  11. // 第一个命名空间
  12. namespace first_space {
  13. int num = 99;
  14.  
  15. void yongqiang() {
  16. std::cout << "Inside first_space" << std::endl;
  17. std::cout << "num = " << num << std::endl;
  18. }
  19.  
  20. // 第二个命名空间
  21. namespace second_space {
  22. int num = 11;
  23.  
  24. void yongqiang() {
  25. std::cout << "Inside first_space" << std::endl;
  26. std::cout << "num = " << num << std::endl;
  27. }
  28. }
  29. }
  30.  
  31. int main()
  32. {
  33. std::cout << "first_space::num = " << first_space::num << std::endl;
  34. std::cout << "first_space::second_space::num = " << first_space::second_space::num << std::endl;
  35. std::cout << "num = " << num << std::endl;
  36. std::cout << "::num = " << ::num << std::endl;
  37.  
  38. return 0;
  39. }
  40.  

1>------ Build started: Project: hash_table, Configuration: Debug Win32 ------
1>  yongqiang.cpp
1>d:\visual_studio_workspace\hash_table\hash_table\yongqiang.cpp(35): error C2065: 'num': undeclared identifier
1>d:\visual_studio_workspace\hash_table\hash_table\yongqiang.cpp(36): error C2039: 'num': is not a member of '`global namespace''
1>d:\visual_studio_workspace\hash_table\hash_table\yongqiang.cpp(36): error C2065: 'num': undeclared identifier
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

用 using 来告诉编译器用到的是哪个命名空间内的内容。在 main() 中加 using namespace first_space; 或者 using namespace first_space::second_space;。但是不能同时使用,因为这样也会导致编译出错,编译器不知道要去使用哪个命名空间的变量和函数。

5.1 using namespace first_space;

  1. //============================================================================
  2. // Name : Yongqiang Cheng
  3. // Author : Yongqiang Cheng
  4. // Version : Version 1.0.0
  5. // Copyright : Copyright (c) 2020 Yongqiang Cheng
  6. // Description : Hello World in C++, Ansi-style
  7. //============================================================================
  8.  
  9. #include <iostream>
  10.  
  11. // 第一个命名空间
  12. namespace first_space {
  13. int num = 99;
  14.  
  15. void yongqiang() {
  16. std::cout << "Inside first_space" << std::endl;
  17. std::cout << "num = " << num << std::endl;
  18. }
  19.  
  20. // 第二个命名空间
  21. namespace second_space {
  22. int num = 11;
  23.  
  24. void yongqiang() {
  25. std::cout << "Inside first_space" << std::endl;
  26. std::cout << "num = " << num << std::endl;
  27. }
  28. }
  29. }
  30.  
  31. using namespace first_space;
  32.  
  33. int main()
  34. {
  35. std::cout << "first_space::num = " << first_space::num << std::endl;
  36. std::cout << "first_space::second_space::num = " << first_space::second_space::num << std::endl;
  37. std::cout << "num = " << num << std::endl;
  38. std::cout << "::num = " << ::num << std::endl;
  39.  
  40. return 0;
  41. }
  42.  

first_space::num = 99
first_space::second_space::num = 11
num = 99
::num = 99
请按任意键继续. . .

5.2 using namespace first_space::second_space;

  1. //============================================================================
  2. // Name : Yongqiang Cheng
  3. // Author : Yongqiang Cheng
  4. // Version : Version 1.0.0
  5. // Copyright : Copyright (c) 2020 Yongqiang Cheng
  6. // Description : Hello World in C++, Ansi-style
  7. //============================================================================
  8.  
  9. #include <iostream>
  10.  
  11. // 第一个命名空间
  12. namespace first_space {
  13. int num = 99;
  14.  
  15. void yongqiang() {
  16. std::cout << "Inside first_space" << std::endl;
  17. std::cout << "num = " << num << std::endl;
  18. }
  19.  
  20. // 第二个命名空间
  21. namespace second_space {
  22. int num = 11;
  23.  
  24. void yongqiang() {
  25. std::cout << "Inside first_space" << std::endl;
  26. std::cout << "num = " << num << std::endl;
  27. }
  28. }
  29. }
  30.  
  31. using namespace first_space::second_space;
  32.  
  33. int main()
  34. {
  35. std::cout << "first_space::num = " << first_space::num << std::endl;
  36. std::cout << "first_space::second_space::num = " << first_space::second_space::num << std::endl;
  37. std::cout << "num = " << num << std::endl;
  38. std::cout << "::num = " << ::num << std::endl;
  39.  
  40. return 0;
  41. }
  42.  

first_space::num = 99
first_space::second_space::num = 11
num = 11
::num = 11
请按任意键继续. . .

References

https://www.runoob.com/cplusplus/cpp-tutorial.html

总结

到此这篇关于C++语言中命名空间 (namespace)的文章就介绍到这了,更多相关C++命名空间 (namespace)内容请搜索w3xue以前的文章或继续浏览下面的相关文章希望大家以后多多支持w3xue!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号