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. 【AI测试】人工智能 (AI) 测试--开篇

    人工智能测试 什么是人工智能,人工智能是怎么测试的.可能是大家一开始最想了解的.大家看图中关于人工智能的定义.通俗点来说呢,就是 让机器实现原来只有人类才能完成的任务:比如看懂照片,听懂说话,思考等等 ...

  2. 元素显示模式:块元素 & 行内元素 & 行内块元素

    元素显示模式 前言 了解元素的显示模式可以更好的让我们布局页面.了解显示模式需要学习以下三个方面 什么是元素的显示模式 元素显示模式的分类 元素显示模式的转换 什么是元素显示模式 元素显示模式就是元素 ...

  3. Linux Mem (目录)

    1.用户态相关: 1.1.用户态进程空间的创建 - execve() 详解 1.2.用户态进程空间的映射 - mmap()详解 1.3.分页寻址(Paging/MMU)机制详解 2.内核态相关: 2. ...

  4. 『与善仁』Appium基础 — 7、ADB Shell命令的使用

    目录 1.查看进程 2.查看实时资源占用情况 3.查看进程 UID 4.其它ADB Shell命令说明 Android系统是基于 Linux 内核的,也就是说Android系统的底层是Linux系统. ...

  5. c++学习笔记4(函数重载)

    一个或多个函数,名字相似,然而参数个数或类型不同,这个叫做函数重载 优点:可以使函数的命名变得简单

  6. SpringCloud微服务实战——搭建企业级开发框架(十六):集成Sentinel高可用流量管理框架【自定义返回消息】

    Sentinel限流之后,默认的响应消息为Blocked by Sentinel (flow limiting),对于系统整体功能提示来说并不统一,参考我们前面设置的统一响应及异常处理方式,返回相同的 ...

  7. c++ 中vector 常见用法(给初学者)

    c++ 中 vector vector有两个参数,一个是size,表示当前vector容器内存储的元素个数,一个是capacity,表示当前vector在内存中申请的这片区域所能容纳的元素个数. ca ...

  8. 【数据结构】【图文】【oj习题】 图的拓扑排序(邻接表)

    拓扑排序: 按照有向图给出的次序关系,将图中顶点排成一个线性序列,对于有向图中没有限定次序关系的顶点,则可以人为加上任意的次序关系,由此所得顶点的线性序列称之为拓扑有序序列.显然对于有回路的有向图得不 ...

  9. [gym102268E]Expected Value

    令$X$为移动次数,答案即$\sum_{i=0}^{\infty}P(X>i)$,后者记作$S_{i}$ 关于$S_{i}$,令$f_{i,j}$表示走了$i$步后位于$j$且未到达过$k$的概 ...

  10. COS 音视频实践 | 多种姿势让你的视频“跑”起来

    导语 随着4G/5G时代的到来,短视频/直播行业开始流行,音视频逐渐成为信息传播中流量占比最大的部分.腾讯云对象存储(COS)作为可容纳海量数据且支持 HTTP/HTTPS 协议访问的分布式存储服务, ...