前言
命名空间可作为附加信息来区分不同库中相同名称的函数、类、变量等。命名空间即定义了上下文,命名空间就是定义了一个范围。
一个文件夹 (目录) 中可以包含多个文件夹,每个文件夹中不能有相同的文件名,但不同文件夹中的文件可以重名。
1. 命名空间
命名空间的定义使用关键字 namespace,后跟命名空间的名称。
- namespace namespace_name {
- // 代码声明
- }
为了调用带有命名空间的函数或变量,需要在前面加上命名空间的名称。
- namespace_name::yongqiang; // yongqiang 可以是变量或函数
- //============================================================================
- // Name : Yongqiang Cheng
- // Author : Yongqiang Cheng
- // Version : Version 1.0.0
- // Copyright : Copyright (c) 2020 Yongqiang Cheng
- // Description : Hello World in C++, Ansi-style
- //============================================================================
-
- #include <iostream>
-
- // 第一个命名空间
- namespace first_space {
- void yongqiang() {
- std::cout << "Inside first_space" << std::endl;
- }
- }
-
- // 第二个命名空间
- namespace second_space {
- void yongqiang() {
- std::cout << "Inside second_space" << std::endl;
- }
- }
-
- int main()
- {
- // 调用第一个命名空间中的函数
- first_space::yongqiang();
-
- // 调用第二个命名空间中的函数
- second_space::yongqiang();
-
- return 0;
- }
-
Inside first_space
Inside second_space
请按任意键继续. . .
2. using 指令
using namespace 指令会告诉编译器,后续的代码将使用指定的命名空间中的名称。
- //============================================================================
- // Name : Yongqiang Cheng
- // Author : Yongqiang Cheng
- // Version : Version 1.0.0
- // Copyright : Copyright (c) 2020 Yongqiang Cheng
- // Description : Hello World in C++, Ansi-style
- //============================================================================
-
- #include <iostream>
-
- using namespace std;
-
- // 第一个命名空间
- namespace first_space {
- void yongqiang() {
- cout << "Inside first_space" << endl;
- }
- }
-
- // 第二个命名空间
- namespace second_space {
- void yongqiang() {
- cout << "Inside second_space" << endl;
- }
- }
-
- using namespace first_space;
-
- int main()
- {
- // 调用第一个命名空间中的函数
- first_space::yongqiang();
-
- // 调用第二个命名空间中的函数
- second_space::yongqiang();
-
- // 调用第一个命名空间中的函数
- yongqiang();
-
- return 0;
- }
-
Inside first_space
Inside second_space
Inside first_space
请按任意键继续. . .
using 指令用来指定命名空间中的特定项目,
使用 std 命名空间中的 cout 部分,在使用 cout 时就可以不用加上命名空间名称作为前缀,但是 std 命名空间中的其他项目仍然需要加上命名空间名称作为前缀。
- //============================================================================
- // Name : Yongqiang Cheng
- // Author : Yongqiang Cheng
- // Version : Version 1.0.0
- // Copyright : Copyright (c) 2020 Yongqiang Cheng
- // Description : Hello World in C++, Ansi-style
- //============================================================================
-
- #include <iostream>
-
- using std::cout;
-
- int main()
- {
- cout << "yongqiang cheng!" << std::endl;
-
- return 0;
- }
yongqiang cheng!
请按任意键继续. . .
using 指令引入的名称遵循正常的范围规则。名称从使用 using 指令开始是可见的,直到该范围结束。在范围以外定义的同名实体是隐藏的。
3. 不连续的命名空间
命名空间可以定义在几个不同的部分中,因此命名空间是由几个单独定义的部分组成的。一个命名空间的各个组成部分可以分散在多个文件中。
如果命名空间中的某个组成部分需要请求定义在另一个文件中的名称,则仍然需要声明该名称。下面的命名空间定义可以是定义一个新的命名空间,也可以是为已有的命名空间增加新的元素:
- namespace namespace_name {
- // 代码声明
- }
4. 嵌套的命名空间
命名空间可以嵌套,您可以在一个命名空间中定义另一个命名空间。
- namespace namespace_name1 {
- // 代码声明
- namespace namespace_name2 {
- // 代码声明
- }
- }
使用 :: 运算符来访问嵌套的命名空间中的成员。
- // 访问 namespace_name2 中的成员
- using namespace namespace_name1::namespace_name2;
-
- // 访问 namespace:name1 中的成员
- using namespace namespace_name1;
如果使用的是 namespace_name1,那么在该范围内 namespace_name2 中的元素也是可用的。
- //============================================================================
- // Name : Yongqiang Cheng
- // Author : Yongqiang Cheng
- // Version : Version 1.0.0
- // Copyright : Copyright (c) 2020 Yongqiang Cheng
- // Description : Hello World in C++, Ansi-style
- //============================================================================
-
- #include <iostream>
-
- using namespace std;
-
- // 第一个命名空间
- namespace first_space {
- void yongqiang() {
- cout << "Inside first_space" << endl;
- }
-
- // 第二个命名空间
- namespace second_space {
- void yongqiang() {
- cout << "Inside second_space" << endl;
- }
- }
- }
-
- using namespace first_space::second_space;
-
- int main()
- {
- // 调用第一个命名空间中的函数
- first_space::yongqiang();
-
- // 调用第二个命名空间中的函数
- first_space::second_space::yongqiang();
-
- // 调用第二个命名空间中的函数
- yongqiang();
-
- return 0;
- }
-
Inside first_space
Inside second_space
Inside second_space
请按任意键继续. . .
5. 命名空间内变量、函数、全局变量的作用域
- //============================================================================
- // Name : Yongqiang Cheng
- // Author : Yongqiang Cheng
- // Version : Version 1.0.0
- // Copyright : Copyright (c) 2020 Yongqiang Cheng
- // Description : Hello World in C++, Ansi-style
- //============================================================================
-
- #include <iostream>
-
- // 第一个命名空间
- namespace first_space {
- int num = 99;
-
- // 第二个命名空间
- namespace second_space {
- int num = 11;
- }
- }
-
- // 全局变量
- int num = 55;
-
- int main()
- {
- std::cout << "first_space::num = " << first_space::num << std::endl;
- std::cout << "first_space::second_space::num = " << first_space::second_space::num << std::endl;
- std::cout << "num = " << num << std::endl;
- std::cout << "::num = " << ::num << std::endl;
-
- int num = 77;
- std::cout << "num = " << num << std::endl;
- std::cout << "::num = " << ::num << std::endl;
-
- return 0;
- }
-
全局变量 num 表达为 ::num,当有同名的局部变量时来区别两者。
first_space::num = 99
first_space::second_space::num = 11
num = 55
::num = 55
num = 77
::num = 55
请按任意键继续. . .
- //============================================================================
- // Name : Yongqiang Cheng
- // Author : Yongqiang Cheng
- // Version : Version 1.0.0
- // Copyright : Copyright (c) 2020 Yongqiang Cheng
- // Description : Hello World in C++, Ansi-style
- //============================================================================
-
- #include <iostream>
-
- // 第一个命名空间
- namespace first_space {
- int num = 99;
-
- void yongqiang() {
- std::cout << "Inside first_space" << std::endl;
- std::cout << "num = " << num << std::endl;
- }
-
- // 第二个命名空间
- namespace second_space {
- int num = 11;
-
- void yongqiang() {
- std::cout << "Inside first_space" << std::endl;
- std::cout << "num = " << num << std::endl;
- }
- }
- }
-
- int main()
- {
- std::cout << "first_space::num = " << first_space::num << std::endl;
- std::cout << "first_space::second_space::num = " << first_space::second_space::num << std::endl;
- std::cout << "num = " << num << std::endl;
- std::cout << "::num = " << ::num << std::endl;
-
- return 0;
- }
-
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;
- //============================================================================
- // Name : Yongqiang Cheng
- // Author : Yongqiang Cheng
- // Version : Version 1.0.0
- // Copyright : Copyright (c) 2020 Yongqiang Cheng
- // Description : Hello World in C++, Ansi-style
- //============================================================================
-
- #include <iostream>
-
- // 第一个命名空间
- namespace first_space {
- int num = 99;
-
- void yongqiang() {
- std::cout << "Inside first_space" << std::endl;
- std::cout << "num = " << num << std::endl;
- }
-
- // 第二个命名空间
- namespace second_space {
- int num = 11;
-
- void yongqiang() {
- std::cout << "Inside first_space" << std::endl;
- std::cout << "num = " << num << std::endl;
- }
- }
- }
-
- using namespace first_space;
-
- int main()
- {
- std::cout << "first_space::num = " << first_space::num << std::endl;
- std::cout << "first_space::second_space::num = " << first_space::second_space::num << std::endl;
- std::cout << "num = " << num << std::endl;
- std::cout << "::num = " << ::num << std::endl;
-
- return 0;
- }
-
first_space::num = 99
first_space::second_space::num = 11
num = 99
::num = 99
请按任意键继续. . .
5.2 using namespace first_space::second_space;
- //============================================================================
- // Name : Yongqiang Cheng
- // Author : Yongqiang Cheng
- // Version : Version 1.0.0
- // Copyright : Copyright (c) 2020 Yongqiang Cheng
- // Description : Hello World in C++, Ansi-style
- //============================================================================
-
- #include <iostream>
-
- // 第一个命名空间
- namespace first_space {
- int num = 99;
-
- void yongqiang() {
- std::cout << "Inside first_space" << std::endl;
- std::cout << "num = " << num << std::endl;
- }
-
- // 第二个命名空间
- namespace second_space {
- int num = 11;
-
- void yongqiang() {
- std::cout << "Inside first_space" << std::endl;
- std::cout << "num = " << num << std::endl;
- }
- }
- }
-
- using namespace first_space::second_space;
-
- int main()
- {
- std::cout << "first_space::num = " << first_space::num << std::endl;
- std::cout << "first_space::second_space::num = " << first_space::second_space::num << std::endl;
- std::cout << "num = " << num << std::endl;
- std::cout << "::num = " << ::num << std::endl;
-
- return 0;
- }
-
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!