在本章中,我们将学习以下主题:
1.简介
2. Declaring 命名空间s
3. Accessing 命名空间 使用 “using” directive
4. The global 命名空间
5. The 性病 命名空间
6. Nested 命名空间s
7. Inline 命名空间s (C++ 11)
8.命名空间别名
9. Anonymous or unnamed 命名空间s
10. C ++中的不连续命名空间
1.简介
命名空间用于为标识符(例如函数,变量,类等)提供范围,并在不同的库中提供相同的名称。
同一名称空间中的所有标识符都是彼此可见的。
可以通过对每个标识符使用完全限定的名称来访问名称空间范围之外的标识符。例如“std::vector<std::string> vec;”.
在2个不同名称空间中声明的变量可以具有相同的名称。
2. Declaring 命名空间s
“namespace” is the keyword is used to declare a 命名空间.
Below is the way to declare a 命名空间:
命名空间 MyNamespaceName { //code declarations }
范围解析运算符可以访问标识符。
例:
MyNamespaceName::code;
完整的代码示例:
#include <iostream> // for more tutorial in C ++ visit www.prodevelopertutorial.com using 命名空间 性病; // First 命名空间 namespace FirstNameSpace { void func() { cout << "Inside FirstNameSpace" << endl; } } // Second 命名空间 namespace SecondNameSpace { void func() { cout << "Inside SecondNameSpace" << endl; } } int main () { // Calling first 命名空间 FirstNameSpace::func(); // Calling second 命名空间 SecondNameSpace::func(); return 0; }
输出:
Inside FirstNameSpace Inside SecondNameSpace
3. Accessing 命名空间 使用 “using” directive
You can access the 命名空间 by 使用 “using” directive.
#include <iostream> // for more tutorial in C ++ visit www.prodevelopertutorial.com using 命名空间 性病; // First 命名空间 namespace FirstNameSpace { void func() { cout << "Inside FirstNameSpace" << endl; } } using 命名空间 FirstNameSpace; int main () { func(); return 0; }
4. The global 命名空间
在所有名称空间之外定义的标识符将称为全局名称空间。建议不要创建全局名称空间。主函数的入口点例外,它需要全局名称空间才能起作用。
要访问全局名称空间标识符,请使用范围解析运算符“::”.
例如:
::myGlobalFunction();
这将区分全局标识符和具有相同名称的任何其他本地标识符。
5. The 性病 命名空间
所有标准C ++库都在“std”命名空间或嵌套在内部“std” 命名空间.
6. Nested 命名空间s
命名空间可以在另一个命名空间中声明。然后将其称为嵌套名称空间。
例:
#include <iostream> // for more tutorial in C ++ visit www.prodevelopertutorial.com using 命名空间 性病; namespace OutsideNamespace { int a; int b; // Second 命名空间 命名空间 InsideNamespace { void add(int num1, int num2) { cout << "Sum is "<<(num1 + num2) << endl; } } } int main () { OutsideNamespace::a = 10; OutsideNamespace::b = 20; OutsideNamespace::InsideNamespace::add(OutsideNamespace::a, OutsideNamespace::b); return 0; }
输出:
Sum is 30
7. Inline 命名空间s (C++ 11)
Inline 命名空间s are introduced in C ++ 11.
Inline 命名空间s are used as a nested 命名空间.
它们被视为普通名称空间,而不是嵌套名称空间。
例:
#include <iostream> // for more tutorial in C ++ visit www.prodevelopertutorial.com using 命名空间 性病; namespace Foo { // Inline 命名空间 inline 命名空间 Bar { void barFun() { cout << "In bar Func"<< endl; } } } int main () { //barFun can be called with the parent 命名空间 Foo::barFun(); return 0; }
输出:
In bar Func
But what is the use of inline 命名空间?
在版本控制中很有用。假设您发布了该函数的新更新版本,并且该版本不向后兼容。那时您可以使用内联名称空间。
它可以显示如下:
#include <iostream> // for more tutorial in C ++ visit www.prodevelopertutorial.com using 命名空间 性病; namespace Foo { 命名空间 v1 { void fooFun() { cout << "In old foo Func"<< endl; } } // Inline 命名空间 inline 命名空间 v2 { void fooFun() { cout << "In new foo Func"<< endl; } } } int main () { //now if you cann fooFun(), latest v2 fooFun will be called. Foo::fooFun(); //now if the user wants the older function to be called, then he can call as below: Foo::v1::fooFun(); return 0; }
输出:
In new foo Func In old foo Func
8.命名空间别名
You can give another name for a 命名空间.
例:
#include <iostream> // for more tutorial in C ++ visit www.prodevelopertutorial.com using 命名空间 性病; namespace this_namespace_name_is_long { void foo() { cout<<"In foo function"<<endl; } } namespace stortName = this_namespace_name_is_long; int main () { stortName::foo(); return 0; }
输出:
In foo function
9. Anonymous or unnamed 命名空间s
创建没有任何名称的名称空间称为匿名或未命名的名称空间。
它用于为名称空间内声明的函数和类创建内部链接。可以在声明的文件内部访问它们。
不能从其他文件访问相同的函数和类。
例:
#include <iostream> // for more tutorial in C ++ visit www.prodevelopertutorial.com using 命名空间 性病; namespace { void foo() { cout<<"In foo function"<<endl; } } int main () { foo(); return 0; }
输出:
In foo function
10. C ++中的不连续命名空间
C ++允许您定义分布在各种文件中的相同名称空间。
然后在编译步骤中将其合并。
例:
命名空间 命名空间_name { // declarations }