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

最新文章

  1. JS和ASP.net相互调用问题
  2. js与多行字符串
  3. CodeForces 474B E(Contest #1)
  4. webpack的几个使用方法
  5. [转载]centos安装svn服务器
  6. js中对象概念的声明
  7. ContentType Office
  8. iOS工程如何支持64-bit
  9. robotframework笔记10
  10. Python标准库的学习准备
  11. H.264 Transform
  12. Fork 一个仓库并同步
  13. ImageView的学习
  14. Hadoop源码学习之HDFS(一)
  15. bzoj 2428 均分数据 模拟退火
  16. 大数据学习之路(1)Hadoop生态体系结构
  17. Hadoop-3.0.2 覆盖源代码生效
  18. C# 流总结
  19. Unity3D Shader 高斯模糊
  20. idea 与springboot 快捷键

热门文章

  1. 大数据中必须要掌握的 Flink SQL 详细剖析
  2. RF运行之后控制信息日志显示乱码(解决方法)
  3. (数据科学学习手札130)利用geopandas快捷绘制在线地图
  4. Emmet语法 —— 快速生成HTML结构
  5. Python基础(filter)
  6. 菜鸡的Java笔记 第五 - java 程序逻辑控制
  7. Json数据使用及学习方法
  8. 填坑总结:python内存泄漏排查小技巧
  9. 【原创】【自制系列】自制stack类型(泛型)
  10. 全面了解一致性哈希算法及PHP代码实现