C++ namespace
Suppose there is a situation when there are two students named Zara in a class. In order to clearly distinguish them, we have to use some additional information in addition to their names, such as their families. address, or their parents’ names, etc.
The same situation occurs in C++ applications. For example, you might write a function called xyz() and an identical function xyz() exists in another available library. This way the compiler cannot tell which xyz() function you are using.
Therefore, the concept of namespace is introduced specifically to solve the above problems. It can be used as additional information to distinguish functions, classes, variables, etc. with the same name in different libraries. Using a namespace defines a context. Essentially, a namespace defines a scope.
Define a namespace
A namespace is defined using the keyword namespace, followed by the name of the namespace, as follows:
namespace namespace_name { // 代码声明 }
In order to call with Functions or variables with a namespace need to be preceded by the name of the namespace, as shown below:
name::code; // code 可以是变量或函数
Let's take a look at how namespaces define scopes for entities such as variables or functions:
#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 will produce the following results:
Inside first_space Inside second_space
using directive
You can use the using namespace directive, so that when using When using a namespace, you do not need to add the name of the namespace in front. This directive tells the compiler that subsequent code will use names in the specified namespace.
#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; }
When the above code is compiled and executed, it produces the following results:
Inside first_space
The using directive can also be used to specify specific items in a namespace. For example, if you only plan to use the cout part of the std namespace, you can use the following statement:
using std::cout;
In the subsequent code, you do not need to prefix the namespace name when using cout, but std Other items in the namespace still need to be prefixed with the namespace name, as shown below:
#include <iostream> using std::cout; int main () { cout << "std::endl is used with std!" << std::endl; return 0; }
When the above code is compiled and executed, it produces the following results:
std::endl is used with std!
using Names introduced by directives follow normal scoping rules. The name is visible starting with the using directive until the end of the scope. At this time, entities with the same name defined outside the scope are hidden.
Discontinuous namespace
A namespace can be defined in several different parts, so the namespace is composed of several separately defined parts. Various components of a namespace can be spread across multiple files.
So, if a component of the namespace needs to request a name defined in another file, the name still needs to be declared. The following namespace definition can define a new namespace or add new elements to an existing namespace:
namespace namespace_name { // 代码声明 }
Nested namespace
Namespaces can be nested sets, you can define one namespace within another as follows:
namespace namespace_name1 { // 代码声明 namespace namespace_name2 { // 代码声明 } }
You can access members in a nested namespace by using the :: operator:
// 访问 namespace_name2 中的成员 using namespace namespace_name1::namespace_name2; // 访问 namespace:name1 中的成员 using namespace namespace_name1;
In the above statement, if namespace_name1 is used, then in the scope namespace_name2 Elements 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; }
When the above code is compiled and executed, it produces the following results:
Inside second_space