Nested Classes in C++
A nested class is a class which is declared in another enclosing class. A nested class is a member and as such has the same access rights as any other member. The members of an enclosing class have no special access to members of a nested class; the usual access rules shall be obeyed.
For example, program 1 compiles without any error and program 2 fails in compilation.
Program1
- 1 //Program 1
- 2
- 3 #include<iostream>
- 4
- 5 using namespace std;
- 6
- 7 /* start of Enclosing class declaration */
- 8 class Enclosing
- 9 {
- 10 public: //if remove the keyword, may be wrong in some compiler
- 11 int x;
- 12
- 13 /* start of Nested class declaration */
- 14 class Nested
- 15 {
- 16 int y;
- 17 void NestedFun(Enclosing *e)
- 18 {
- 19 cout << e->x; // works fine: nested class can access private members of Enclosing class
- 20 }
- 21 }; // declaration Nested class ends here
- 22 }; // declaration Enclosing class ends here
- 23
- 24 int main()
- 25 {
- 26
- 27 }
Program 2
- 1 #include<iostream>
- 2
- 3 using namespace std;
- 4
- 5 /* start of Enclosing class declaration */
- 6 class Enclosing
- 7 {
- 8
- 9 int x;
- 10
- 11 /* start of Nested class declaration */
- 12 class Nested
- 13 {
- 14 int y;
- 15 }; // declaration Nested class ends here
- 16
- 17 void EnclosingFun(Nested *n)
- 18 {
- 19 cout << n->y; // Compiler Error: y is private in Nested
- 20 }
- 21 }; // declaration Enclosing class ends here
- 22
- 23 int main()
- 24 {
- 25
- 26 }
References: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1905.pdf
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:40:49
Nested Classes in C++的更多相关文章
- Java - Nested Classes
(本文参考:http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html) Nested Classes class OuterClas ...
- Java Nested Classes(内部类~第一篇英文技术文档翻译)
鄙人最近尝试着翻译了自己的第一篇英文技术文档.Java Nested Classes Reference From Oracle Documentation 目录 嵌套类-Nested Classes ...
- Nested Classes
http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html package priceton; /* * Copyright (c) ...
- Java中的Nested Classes和Inner Classes
Java中的Nested Classes和Inner Classes Java有嵌套类(Nested Classes)和内部类(Inner Classes)的概念. 嵌套类(Nested Classe ...
- Effective Java 13 Minimize the accessibility of classes and members
Information hiding is important for many reasons, most of which stem from the fact that it decouples ...
- Effetive Java 22 Favor static member classes over nonstatic
Nested class types Usage and remark Advantage Disadvantage static member classes Use for public help ...
- Visual C++ 里的 Classes, Methods and RTTI
类的基本布局 为了说明以下内容,让我们考虑这个简单的例子: class A { int a1; public: virtual int A_virt1(); virtual int A_virt2() ...
- Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(十)之Inner Classes
The inner class is a valuable feature because it allows you to group classes that logically belong t ...
- .NET平台开源项目速览(18)C#平台JSON实体类生成器JSON C# Class Generator
去年,我在一篇文章用原始方法解析复杂字符串,json一定要用JsonMapper么?中介绍了简单的JSON解析的问题,那种方法在当时的环境是非常方便的,因为不需要生成实体类,结构很容易解析.但随着业务 ...
随机推荐
- 模块化开发 | es6模块暴露与引入
CommonJS模块开发 CommonJS定义 每个文件就一个模块,有自己的作用域.在一个文件里面定义的变量.函数.类,都是私有的,对其他文件不可见. 私有作用域不会污染全局作用域. 模块可加载多次, ...
- uni-app app端设置全屏背景色
设置page:{样式},博主调试的时候在app端不起作用,设置配置文件的backgroundColor也没有用,所以博主就使用了一个稍微比较偏的办法解决了,没有用获取设备信息的api来实现 具体操作就 ...
- requestAnimationFrame 执行机制探索
1.什么是 requestAnimationFrame window.requestAnimationFrame() 告诉浏览器--你希望执行一个动画,并且要求浏览器在下次重绘之前调用指定的回调函数更 ...
- coding game, 边打游戏边学编程,是一种怎么样的体验?
前言 hello,大家好,我是bigsai,好久不见,甚是想念! 在日常生活中,很多人喜欢玩游戏,因为游戏中有着对抗博弈.控制的喜悦,用灵魂指法完成一波靓丽的操作. 但实际上,你的按键都是对应代码中一 ...
- Django笔记&教程 6-1 表单(Form)介绍
Django 自学笔记兼学习教程第6章第1节--表单(Form)介绍 点击查看教程总目录 1 介绍 如果网站要让用户输入并提交数据(比如注册登录),则需要用到表单. 单纯的html也能写出表单,格式一 ...
- ubuntu更換清華軟件源
打开软件源的编辑sudo gedit /etc/apt/sources.list 软件源: Ubuntu--更改国内镜像源(阿里.网易.清华.中科大) 打開軟件源文件進行修改: 使用 sudo vim ...
- [atAGC054C]Roughly Sorted
考虑对于确定的排列$\{p_{i}\}$,如何求出其(交换后)会得到的排列-- 令$cnt_{x}$为在$i$之前比$x$大的元素个数(其中$p_{i}=x$),显然排列合法当且仅当$cnt_{i}\ ...
- maven插件慢的解决方案
-DarchetypeCatalog=local 地址:https://www.cnblogs.com/del88/p/6286887.html
- ppt的作用
ppt不重要,使用ppt的场合很重要. 演讲分享,答辩总结,商业竞标,新年计划,年终总结.
- 如何隐藏shell脚本内容
从事 Linux 开发的同学,经常需要编写 shell 脚本,有时脚本中会涉及到一些敏感内容,比如一些 IP 地址,用户名以及密码等,或者脚本中有一些关键的代码, 所有这些内容你都不想别人阅读或者修改 ...