命名空间定义(有三种情况)
定义命名空间,需要使用到namespace关键字,后面跟命名空间的名字,然后接一对{}即可,{}中即为命名空间的成员。
附上代码:
#define _CRT_SECURE_NO_WARNINGS 1 #include<iostream> //命名空间的定义 using namespace std; namespace N1 { int a = 100; namespace N2 { int b = 101; } } Namespace N1 { int c = 102; } int main() { cout << N1::a << endl; cout << N1:: N2::b << endl; cout << N1::c << endl; return 0; } |
注意:一个命名空间就定义了一个新的作用域,命名空间中的所有内容都局限于该命名空间中