一、为什么使用命名空间
考虑一种情况,当我们有两个同名的人,Zara,在同一个班里。当我们需要对它们进行区分我们必须使用一些额外的信息和它们的名字,比如这个区域,如果它们生活在不同的区域或者它们的母亲或父亲的名字,等等。
在您的C++应用程序中也会出现同样的情况。例如,您可能正在编写一些具有名为xyz()函数的代码,并且还有另一个可用的库,它也具有相同的xyz()函数。现在编译器无法知道您在代码中引用的xyz()函数的哪个版本。
名称空间(namespace)被设计来克服这个困难,并被用作额外的信息来区分类似的函数、类、变量等等,它们在不同的库中具有相同的名称。使用名称空间,您可以定义定义名称的上下文。本质上,名称空间定义了一个范围。
二、命名空间的定义
在C语言中只有一个全局作用域:
1.C语言中所有的全局标识符共享一个作用域
2.标识符之间可能发生冲突
C++中提出了命名空间的概念:
1.命名空间将全局作用域分成不同的部分,
2.不同命名空间中的标识符可以同名而不会发生冲突
3.命名空间可以发生嵌套
4.全局作用域也叫默认命名空间
Defining a Namespace
A namespace definition begins with the keyword namespace followed by the namespace name as follows −
namespace namespace_name { // code declarations }
To call the namespace-enabled version of either function or variable, prepend (::) the namespace name as follows −
要调用功能或变量的启用名称空间的版本,请在名称空间名称前添加(::),如下所示:
name::code; // code could be variable or function.
Let us see how namespace scope the entities including variable and functions −
让我们看看命名空间如何限制包括变量和函数在内的实体-
#include <iostream> using namespace std; // first name space namespace first_space { void func() { cout << "Inside first_space" << endl; } } // second name space namespace second_space { void func() { cout << "Inside second_space" << endl; } } int main () { // Calls function from first name space. first_space::func(); // Calls function from second name space. second_space::func(); return 0; }
If we compile and run above code, this would produce the following result −
Inside first_space
Inside second_space
还可以避免使用using namespace指令。在名称空间前添加前缀。该指令告诉编译器,后续代码正在使用指定名称空间中的名称。因此,以下代码隐含了名称空间-
#include <iostream> using namespace std; // first name space namespace first_space { void func() { cout << "Inside first_space" << endl; } } // second name space namespace second_space { void func() { cout << "Inside second_space" << endl; } } using namespace first_space; int main () { // This calls function from first name space. func(); return 0; }
If we compile and run above code, this would produce the following result −
Inside first_space
The ‘using’ directive can also be used to refer to a particular item within a namespace. For example, if the only part of the std namespace that you intend to use is cout, you can refer to it as follows −
Subsequent code can refer to cout without prepending the namespace, but other items in the stdnamespace will still need to be explicit as follows −
#include <iostream> using std::cout; int main () { cout << "std::endl is used with std!" << std::endl; return 0; }
If we compile and run above code, this would produce the following result −
std::endl is used with std!
using指令中引入的名称遵循正常的作用域规则。从using指令的点到找到该指令的作用域的结尾,该名称都是可见的。外部作用域中具有相同名称的实体是隐藏的。
总结
C++命名空间的使用:
使用整个命名空间:using namespace name; |
使用命名空间中的变量:using name::variable |
使用默认命名空间中的变量: ::variable |
不连续的命名空间
可以将名称空间分为几部分来定义,因此名称空间由其单独定义的部分之和组成。命名空间的各个部分可以分布在多个文件中。 因此,如果名称空间的一部分需要在另一个文件中定义的名称,则仍必须声明该名称。编写以下命名空间定义,或者定义新的命名空间,或者将新元素添加到现有的命名空间中-
namespace namespace_name { // code declarations }
嵌套命名空间 可以嵌套命名空间
可以在其中定义另一个命名空间中的一个命名空间,如下所示:
namespace namespace_name1 { // code declarations namespace namespace_name2 { // code declarations } }
可以通过使用解析运算符来访问嵌套名称空间的成员,如下所示:
// to access members of namespace_name2 using namespace namespace_name1::namespace_name2; // to access members of namespace:name1 using namespace namespace_name1;
在上面的语句中,如果使用namespace_name1,则它将使scope_name2的元素在范围内可用,如下所示:
#include <iostream> using namespace std; // first name space namespace first_space { void func() { cout << "Inside first_space" << endl; } // second name space namespace second_space { void func() { cout << "Inside second_space" << endl; } } } using namespace first_space::second_space; int main () { // This calls function from second name space. func(); return 0; }
If we compile and run above code, this would produce the following result −
Inside second_space
参考自https://www.tutorialspoint.com/cplusplus/cpp_namespaces.htm