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

Can namespaces be nested in C++?的更多相关文章

  1. Python Scopes and Namespaces

    Before introducing classes, I first have to tell you something about Python's scope rules. Class def ...

  2. (转) ROS NAMING AND NAMESPACES

    原文地址:http://nootrix.com/2013/08/ros-namespaces/   In this tutorial, we will be talking about ROS nam ...

  3. [转]JavaScript Namespaces and Modules

    Namespaces In most programming languages we know the concept of namespaces (or packages).Namespaces ...

  4. [轉]User namespaces – available to play!

    User namespaces – available to play! Posted on May 10, 2012by s3hh Over the past few months, Eric Bi ...

  5. Nested Loops join时显示no join predicate原因分析以及解决办法

    本文出处:http://www.cnblogs.com/wy123/p/6238844.html 最近遇到一个存储过程在某些特殊的情况下,效率极其低效, 至于底下到什么程度我现在都没有一个确切的数据, ...

  6. Error on line -1 of document : Premature end of file. Nested exception: Premature end of file.

    启动tomcat, 出现, ( 之前都是好好的... ) [lk ] ERROR [08-12 15:10:02] [main] org.springframework.web.context.Con ...

  7. SQL Tuning 基础概述06 - 表的关联方式:Nested Loops Join,Merge Sort Join & Hash Join

    nested loops join(嵌套循环)   驱动表返回几条结果集,被驱动表访问多少次,有驱动顺序,无须排序,无任何限制. 驱动表限制条件有索引,被驱动表连接条件有索引. hints:use_n ...

  8. [LeetCode] Nested List Weight Sum II 嵌套链表权重和之二

    Given a nested list of integers, return the sum of all integers in the list weighted by their depth. ...

  9. [LeetCode] Flatten Nested List Iterator 压平嵌套链表迭代器

    Given a nested list of integers, implement an iterator to flatten it. Each element is either an inte ...

随机推荐

  1. 几十行js实现很炫的canvas交互特效

    几十行js实现很炫的canvas交互特效 废话不多说,先上效果图! 本篇文章的示例代码都是抄的一个叫Franks的老外在yutube上的一个教学视频,他还出了很多关于canvas的视频,十分值得学习, ...

  2. 攻防世界 WEB 高手进阶区 easytornado Writeup

    攻防世界 WEB 高手进阶区 easytornado Writeup 题目介绍 题目考点 Python模板 tornado 模板注入 Writeup 进入题目, 目录遍历得到 /flag.txt /w ...

  3. Java测试开发--Spring Tools Suite (STS) 简介(一)

    sts是一个定制版的Eclipse,专为Spring开发定制的,方便创建调试运行维护Spring应用. 官网下载之后,可以看到一个sts-bundle,里面有三个文件夹,一个法律信息,一个tc Ser ...

  4. VMware软件虚拟机不能全屏的问题 & CentOS 安装Vmware Tools

    修改设置 1) 如下图右单击虚拟机名,选择[settings-],调出虚拟机设置界面. 2) 在设置界面选择[hardware]->[CD/DVD2(IDE)]->[Connection] ...

  5. grep命令详解与正则表达式

    grep命令主要是做什么的呢 ?下面我们就来研究下. grep命令简单来说就是"过滤".就是把想看的数据通过grep过滤出来,把不想看的通过grep过滤掉. 它是一种强大的文本搜索 ...

  6. PLSQL批量执行SQL文件方法

    当需要执行多个sql文件,或者某个脚本中,sql语句数量很多时,手动逐个逐条执行不是一个明智的选择. PLSQL为我们提供了便捷的工具.使用方式如下: [工具]--[导入表]--[SQL插入]--[选 ...

  7. myeclipse自带tomcat

    安装myeclipse自带的tomcat没有在myeclipse的安装目录下,是再myeclipse指定的工作空间下 的.metadata\.plugins\com.genuitec.eclipse. ...

  8. JVM的Xms Xmx PermSize MaxPermSize区别

    Eclipse崩溃,错误提示:MyEclipse has detected that less than 5% of the 64MB of Perm Gen (Non-heap memory) sp ...

  9. Hibernate数据校验简介

    我们在业务中经常会遇到参数校验问题,比如前端参数校验.Kafka消息参数校验等,如果业务逻辑比较复杂,各种实体比较多的时候,我们通过代码对这些数据一一校验,会出现大量的重复代码以及和主要业务无关的逻辑 ...

  10. html+css第四篇

    浮动 float浮动: 1.块在一排显示 2.内联支持宽高 3.默认内容撑开宽度 4.脱离文档流 5.提升层级半层 float:left | right | none | inherit; 文档流是文 ...