Can namespaces be nested in C++?
In C++, namespaces can be nested, and resolution of namespace variables is hierarchical.
For example, in the following code, namespace inner is created inside namespace outer, which is inside the global namespace. In the line “int z = x”, x refers to outer::x. If x would not have been in outer then this x would have referred to x in global namespace.
1 #include <iostream>
2 using namespace std;
3
4 int x = 20;
5 namespace outer
6 {
7 int x = 10;
8 namespace inner
9 {
10 int z = x; // this x refers to outer::x
11 }
12 }
13
14 int main()
15 {
16 std::cout<<outer::inner::z; //prints 10
17 getchar();
18 return 0;
19 }
Output of the above program is 10.
On a side node, unlike C++ namespaces, Java packages are not hierarchical.
Also in C++, namespaces are the only entity that allowed to span across multiple files. No other syntactic rules are allowed. For example, we can't split class definition across files nor structure definition.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
转载请注明:http://www.cnblogs.com/iloveyouforever/
2013-11-27 14:44:04
最新文章
- JS和ASP.net相互调用问题
- js与多行字符串
- CodeForces 474B E(Contest #1)
- webpack的几个使用方法
- [转载]centos安装svn服务器
- js中对象概念的声明
- ContentType Office
- iOS工程如何支持64-bit
- robotframework笔记10
- Python标准库的学习准备
- H.264 Transform
- Fork 一个仓库并同步
- ImageView的学习
- Hadoop源码学习之HDFS(一)
- bzoj 2428 均分数据 模拟退火
- 大数据学习之路(1)Hadoop生态体系结构
- Hadoop-3.0.2 覆盖源代码生效
- C# 流总结
- Unity3D Shader 高斯模糊
- idea 与springboot 快捷键