Directory
- C + + namespaces
- I. C + + namespace concept
- Ii. defining namespaces
- Iii. introduction of using directives
- Four, disjoint namespaces
- Five, nested namespaces
C + + namespaces
Introduction: The following excerpt from the C + + rookie Tutorial
I. C + + namespace concept
- For example: when there are two students named Zara in a class, in order to distinguish them clearly, we have to use some extra information outside the name,
such as their home address, or their parents ' names, and so on.
- In C + + applications: You might write a function called XYZ (), and an identical function, XYZ (), exists in another available library. This way, the compiler will not be able to tell which XYZ () function you are using. Therefore, the concept of namespaces is introduced to solve the above problem, which can be used as additional information to distinguish functions, classes, variables, etc. of the same name in different libraries. The context is defined by using a namespace. In essence, a namespace is defined as a scope.
Ii. defining namespaces
namespace namespace_name { // 代码声明}为了调用带有命名空间的函数或变量,需要在前面加上命名空间的名称,如下所示:name::code; // code 可以是变量或函数
实例#include <iostream>using namespace std;// 第一个命名空间namespace first_space{ void func(){ cout << "Inside first_space" << endl;}}// 第二个命名空间namespace second_space{ void func(){ cout << "Inside second_space" << endl;}}int main (){ // 调用第一个命名空间中的函数 first_space::func(); // 调用第二个命名空间中的函数 second_space::func(); return 0;}
When the above code is compiled and executed, it produces the following results:
Inside First_space
Inside Second_space
Iii. introduction of using directives
- You can use the using namespace directive so that you can use a namespace without having to precede the name of the namespace. This instruction tells the compiler that the
后续的代码
name in the specified namespace will be used.
实例#include <iostream>using namespace std;// 第一个命名空间namespace first_space{ void func() { cout << "Inside first_space" << endl; }}// 第二个命名空间namespace second_space{ void func() { cout << "Inside second_space" << endl; }}using namespace first_space;int main (){ // 调用第一个命名空间中的函数 func(); return 0;}当上面的代码被编译和执行时,它会产生下列结果:Inside first_space
- The using directive can also be used to specify specific items in a namespace. For example, if you only intend to use the cout part of the Std namespace, you can use the following statement:
using std::cout;
- Note: In the subsequent code, you can use cout without a namespace name as a prefix, but other items in the Std namespace still need to be prefixed with the namespace name.
实例#include <iostream>using std::cout;int main (){ cout << "std::endl is used with std!" << std::endl; return 0;}当上面的代码被编译和执行时,它会产生下列结果:std::endl is used with std!
Four, disjoint namespaces
- Namespaces can be defined in several different files, and each component of a namespace can be scattered across multiple files. The following namespace definitions can be understood either as defining a new namespace or adding a new element to an existing namespace:
namespace namespace_name { // 代码声明}
Five, nested namespaces
- Namespaces can be nested, and you can define another namespace in one namespace, as follows:
namespace namespace_name1 { // 代码声明 namespace namespace_name2 { // 代码声明 }}
- You can access members in a nested namespace by using the:: operator:
using namespace namespace_name1::namespace_name2; // 访问 namespace_name2 中的成员using namespace namespace_name1; // 访问 namespace:name1 中的成员
Note: If you are using namespace_name1, the elements within the namespace_name2 are also available, as follows:
实例#include <iostream>using namespace std;// 第一个命名空间namespace first_space{ void func() { cout << "Inside first_space" << endl; }// 第二个命名空间 namespace second_space { void func() { cout << "Inside second_space" << endl; } }}using namespace first_space::second_space;int main (){ // 调用第二个命名空间中的函数 func(); return 0;}当上面的代码被编译和执行时,它会产生下列结果:Inside second_space
The namespace of C + +