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
Can namespaces be nested in C++?的更多相关文章
- Python Scopes and Namespaces
Before introducing classes, I first have to tell you something about Python's scope rules. Class def ...
- (转) ROS NAMING AND NAMESPACES
原文地址:http://nootrix.com/2013/08/ros-namespaces/ In this tutorial, we will be talking about ROS nam ...
- [转]JavaScript Namespaces and Modules
Namespaces In most programming languages we know the concept of namespaces (or packages).Namespaces ...
- [轉]User namespaces – available to play!
User namespaces – available to play! Posted on May 10, 2012by s3hh Over the past few months, Eric Bi ...
- Nested Loops join时显示no join predicate原因分析以及解决办法
本文出处:http://www.cnblogs.com/wy123/p/6238844.html 最近遇到一个存储过程在某些特殊的情况下,效率极其低效, 至于底下到什么程度我现在都没有一个确切的数据, ...
- 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 ...
- SQL Tuning 基础概述06 - 表的关联方式:Nested Loops Join,Merge Sort Join & Hash Join
nested loops join(嵌套循环) 驱动表返回几条结果集,被驱动表访问多少次,有驱动顺序,无须排序,无任何限制. 驱动表限制条件有索引,被驱动表连接条件有索引. hints:use_n ...
- [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. ...
- [LeetCode] Flatten Nested List Iterator 压平嵌套链表迭代器
Given a nested list of integers, implement an iterator to flatten it. Each element is either an inte ...
随机推荐
- connect & send 在三次握手过程中的有趣问题
一.问题回顾 面试的时候被问到的问题,原问题是: 1:写一下socket网络编程服务端和客户端常用的函数. 2:如果服务端在listen之后没有accept,那客户端的connect会返回吗?为什么? ...
- GDB 调试技巧(不断更新中......)
一.break到不同类的同名函数 方法: 在函数前面加类名以及作用域运算符 eg : break A::func //break 到类A的func函数 程序如下: //gdb_test.cpp #in ...
- java中的lamda表达式
List操作: 循环: list.forEach((p) -> System.out.printf("%s %s; %n", p.getFirstName(), p.getL ...
- silky微服务的应用服务和服务条目
目录 服务的定义 服务条目 根据服务条目生成WebAPI 服务条目的治理特性 缓存拦截 服务条目的例子 服务的实现 开源地址 在线文档 服务的定义 服务接口是微服务定义服务的基本单位,定义的应用服务接 ...
- Mysql5.7和8.0版本的文件夹版安装教程(整合版,超详细)
安装Mysql(5.7版本) 下载地址在这里可以自选版本,找到合适的版本进行下载 解压安装包 配置环境变量 win+r 输入 sysdm.cpl 点击高级 点击环境变量 新建一个系统变量 变量名为MY ...
- [uoj272]石家庄的工人阶级队伍比较坚强
假设$x,y\in \{0,1,2\}$,则$x$能赢$y$(根据题中定义)当且仅当$x-y\equiv 1(mod\ 3)$ 定义$\ominus$为两数3进制下不退位的减法,$S_{x}$表示$x ...
- [atARC113F]Social Distance
(由于是实数范围,端点足够小,因此区间都使用中括号,且符号取等号) 定义$P(X)$表示$\forall 2\le i\le n,a_{i}-a_{i-1}\ge X$的概率,那么我们所求的也就是$P ...
- nginx反向代理出错:proxy_pass
问题描述: 一台服务器代理访问另一台服务器,代码如下图所示: 重新加载nginx后不会跳到该域名,而是出现error的页面. 查看error.log日志为以下报错: 2021/03/09 23:07: ...
- [USACO07NOV]Cow Relays G
题目大意 给出一张无向连通图(点数小于1000),求S到E经过k条边的最短路. 算法 这是之前国庆模拟赛的题 因为懒 所以就只挑一些题写博客 在考场上写了个dp 然后水到了50分 出考场和神仙们一问才 ...
- Codeforces 698F - Coprime Permutation(找性质)
Codeforces 题面传送门 & 洛谷题面传送门 u1s1 感觉这个 D1F 比某道 jxd 作业里的 D1F 质量高多了啊,为啥这场的 D 进了 jxd 作业而这道题没进/yun 首先这 ...