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 ...
随机推荐
- 黑客是如何利用DNS域传送漏洞进行渗透与攻击的?
一.DNS域传送 DNS :Domain Name System 一个保存IP地址和域名相互映射关系的分布式数据库,重要的互联网基础设施,默认使用的TCP/UDP端口号是53 常见DNS记录类型: 1 ...
- for和while的区别及使用
for for的定义,()内的三段表达式,除了中间的必须产生布尔型,并未对其余两段有所限制,只要是表达式就可以了. //递增和递减 for(int i = 0;i < 100;i++) for ...
- 第四周PTA笔记 好吃的巧克力+特殊的翻译+下次一定(续)+走迷宫
好吃的巧克力 超市正在特价售卖巧克力,正好被贪吃的Lucky_dog看见了. 巧克力从左到右排成一排,一共有N个,M种. 超市有一个很奇怪的规定,就是你在购买巧克力时必须提供两个数字a和b,代表你要购 ...
- git修改用户和邮箱
GIT查看当前用户以及邮箱 $ git config user.name $ git config user.email GIT修改用户以及邮箱 $ git config --global user. ...
- IIS设置URL重写,实现页面的跳转的重定向方法
默认IIS是不提供URL重写模块的. 请注意,不要将IIS默认的HTTP重定向理解为url重写. 安装url重写模块 url重写,是要从iis的应用市场下载url重写组件才可以的. URL重写工具的下 ...
- 菜鸡的Java笔记 第七 - java 数组
数组的基本概念 笔试中经常出现的试题或是问题 1.1 概念 数组指的是一组相关变量的集合 如果用最原始的方式定义100个变量的话没问题但是这些变量的关联实在 ...
- Vue安装Vue Devtools调试工具提示 Error: Cannot find module '@vue-devtools/build-tools' 解决办法
我看网络上面安装Vue Devtools 调试工具的步骤几乎都是按照文章链接里的步骤进行安装, 但是在最终执行编译命令的时候 :npm run build ,提示如下错误: 尝试了很多方法,都不能解决 ...
- 洛谷 P5469 - [NOI2019] 机器人(区间 dp+拉格朗日插值)
洛谷题面传送门 神仙题,放在 D1T2 可能略难了一点( 首先显然对于 P 型机器人而言,将它放在 \(i\) 之后它会走到左边第一个严格 \(>a_i\) 的位置,对于 Q 型机器人而言,将它 ...
- 如何使用scp在Linux服务器的后台传输文件?
目录 一.上传 常规操作 建议 后台运行 二.下载 两台服务器间文件如何传输?对于小文件,可以先从Linux服务器传到window,再传到另一台服务器.对于大的文件,如测序数据.比对文件等.这样的方法 ...
- 利用elliipse做相关图
参考资料:<数据探掘 R语言实战> p65-P68 install.packages("rattle") # 获取实验数据集 install.packages(&quo ...