C++ 中可以嵌套命名空间吗?
是的,C++ 中可以嵌套命名空间。我们可以按照如下方式在另一个命名空间中定义一个命名空间 -
语法
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;
示例
#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;
}输出
Inside second_space
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP