01 namespace名称空间

xiaoxiao2021-02-27  240

Namespace 名称空间

C语言里,通常只分成全局或局部的变量/函数.在大项目里,当不同开发人员写代码时,会有函数名相同和变量名相同引发的冲突.所以在C源码里,如果变量或函数只是在当前源文件里使用时,可加static修饰符.

C++语言里,可以把全局空间划分成多个子空间(名称空间).在子空间里可以定义类型,函数,声明变量等,而不用管其它子空间里有没有相同的类型,函数及变量名等.也就是每个开发人员使用一个名称空间装载自定义的类型,函数等,这样即可避免冲突的状况.

注意:不属于任何一个名称空间的变量或函数就是属于全局空间的.

访问空间里的成员:空间的名称::成员

1 2 #include <stdio.h> 3 4 namespace myspace { 5 typedef unsigned intu32; 6 int num = 88; 7 void func() 8 { 9 printf("kkkk\n"); 10 } 11 }; 12 13 namespace kkspace { 14 typedef unsigned intu32; 15 int num = 99; 16 void func() 17 { 18 printf("nono\n"); 19 } 20 }; 21 22 int main(void) 23 { 24 myspace::num = 88; 25 kkspace::func(); 26 27 myspace::u32 a = 88; 28 kkspace::u32 b = 11; 29 printf("%d,%d\n", sizeof(a), sizeof(b)); 30 31 return 0; 32 }

名称空间里可以再划分出子空间来.

2 #include <stdio.h> 3 4 namespace myspace { 5 typedef unsigned intu32; 6 int num = 88; 7 void func() 8 { 9 printf("kkkk\n"); 10 } 11 12 namespace myspace2 { 13 void func() { 14 printf("fffffff\n"); 15 } 16 }; 17 };

使用myspace2里的func函数时: myspace::myspace2::func();

使用名称空间,就是相当于把指定空间里的成员变为全局来访问.

用法:using namespace空间名

注意:如在函数体外声明使用名称空间,则整个程序所有函数都可直接访问空间里的成员而不需指定空间名.

如在函数体内声明,则只有此函数可以直接访问空间里的成员.

1 2 #include <stdio.h> 3 4 namespace myspace { 5 void func() 6 { 7 printf("kkkk\n"); 8 } 9 }; 10 11 using namespace myspace; 12 int main(void) 13 { 14 func(); 15 return 0; 16 }

使用名称空间时需要注意不能与全局的变量或函数冲突.如下面myspace空间里有func函数,全局空间也有func函数,调用时就无法区分具体是用哪个函数了,这样是编译不过的.

1 2 #include <stdio.h> 3 4 namespace myspace { 5 void func() 6 { 7 printf("kkkk\n"); 8 } 9 }; 10 11 void func() { 12 printf("globalfunc\n"); 13 } 14 15 using namespace myspace; 16 int main(void) 17 { 18 func(); 19 return 0; 20 } 编译时的错误输出: [root@localhost 01namespace]#g++ 02test.cpp 02test.cpp: In function ‘intmain()’: 02test.cpp:18:7: error: callof overloaded ‘func()’ is ambiguous func(); ^ 02test.cpp:18:7: note:candidates are: 02test.cpp:11:6: note: voidfunc() void func() { ^ 02test.cpp:5:7: note: voidmyspace::func() void func()

解决这问题也可以在调用时直接指定是哪个空间的函数

1 2 #include <stdio.h> 3 4 namespace myspace { 5 void func() 6 { 7 printf("kkkk\n"); 8 } 9 }; 10 11 void func() { 12 printf("globalfunc\n"); 13 } 14 15 using namespace myspace; 16 int main(void) 17 { 18 myspace::func(); 19 ::func(); //全局函数func 20 return 0; 21 } 名称空间里函数体在空间外实现. 空间里先声明函数原型,再在其它地方实现函数体. 1 2 #include <stdio.h> 3 4 namespace myspace { 5 void func(); 6 }; 7 8 int main(void) 9 { 10 using namespacemyspace; 11 12 func(); 13 return 0; 14 } 15 16 17 void myspace::func() 18 { 19 printf("myspacefunc\n"); 20 }

c++里,已把标准输入,输出功能封装到名称空间std里了

std::cin表示输入流对象

std::cout表示输出流对象

std::cerr表示错误输出流对象

std::endl表示换行符

代码:

1 2 #include <iostream> 3 4 int main(void) 5 { 6 using namespace std; 7 int num; 8 9 cin >> num; 10 cout << num <<endl; 11 return 0; 12 } 注意:代码里的”>>”/”<<”不是移位,是表示数据的方向 在/usr/include/c++/4.8.2/iostream头文件里: 42 namespace std_GLIBCXX_VISIBILITY(default) 43 { 44_GLIBCXX_BEGIN_NAMESPACE_VERSION …. 60 extern istream cin; /// Linked to standard input 61 extern ostream cout; /// Linked to standard output 62 extern ostream cerr; /// Linked to standard error (unbuffered) 63 extern ostream clog; /// Linked to standard error (buffered) … 76_GLIBCXX_END_NAMESPACE_VERSION 77 } // namespace

转载请注明原文地址: https://www.6miu.com/read-3977.html

最新回复(0)