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. SkyWalking部署及.Net Core简单使用

    SkyWalking官方网站非常详细,以下只是本人学习过程的整理 一.SkyWalking简介 1.概念 SkyWalking是分布式系统的应用程序性能监视工具,专为微服务.云原生架构而设计 SkyW ...

  2. Iceberg概述

    背景 随着大数据领域的不断发展, 越来越多的概念被提出并应用到生产中而数据湖概念就是其中之一, 其概念参照阿里云的简介: 数据湖是一个集中式存储库, 可存储任意规模结构化和非结构化数据, 支持大数据和 ...

  3. Sonar规范扫描Java代码暴露的问题

    字符串和封装类型应使用 equals()进行比较 例如java.lang.Integer使用引用等于==或!=,因为它不是比较实际值,而是比较内存中的位置. String firstName = ge ...

  4. Python打包成exe,文件太大问题解决办法

    Python打包成exe,文件太大问题解决办法 原因 解决办法 具体步骤 情况一:初次打包 情况二:再次打包 原因 由于使用pyinstaller打包.py文件时,会把很多已安装的无关库同时打包进去, ...

  5. es date_histogram强制补零

    es补零 GET /cars/transactions/_search { "size" : 0, "aggs": { "sales": { ...

  6. c++学习笔记(十)

    返回应用类型 返回引用 1.不要返回局部变量的引用 为了验证为什么不能返回局部变量的引用,我按照所学的例题自己做了一点小测试. #include<iostream> using names ...

  7. 第二周PTA笔记 均等笔+旋转骰子+两个日期相差天数

    均等笔 n个人围成一圈,每人有ai支笔.每人可以向左右相邻的人传递笔,每人每次传递一支笔消耗的能量为1.求使所有人获得均等数量的笔的最小能量. 输入格式: 第一行一个整数n ,表示人的个数(30%的数 ...

  8. int,double与机器字长

    机器字长:计算机能直接处理的二进制数据的位数,它决定了计算机的运算精度想深入了解. 学好汇编语言对你帮助非常大.汇编语言中的,最基本的数据类型有: (1) byte (2)word (3)double ...

  9. [loj3462]括号路径

    对于两条边$(x_{1},y,c)$和$(x_{2},y,c)$,不难发现$x_{1}$与$x_{2}$完全等价,因此可以合并 重复此过程,合并之后用启发式合并来合并边集(注意自环也可以参与合并,即$ ...

  10. c# System.Text.Json 精讲

    本文内容来自我写的开源电子书<WoW C#>,现在正在编写中,可以去WOW-Csharp/学习路径总结.md at master · sogeisetsu/WOW-Csharp (gith ...