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解析的问题,那种方法在当时的环境是非常方便的,因为不需要生成实体类,结构很容易解析.但随着业务 ...
随机推荐
- Red Hat Enterprise Linux (RHEL) 9 更新了什么,即 Rocky Linux 9 和 AlmaLinux 9 展望
请访问原文链接:https://sysin.org/blog/rhel-9-vision/,查看最新版.原创作品,转载请保留出处. 作者:gc(at)sysin.org,主页:www.sysin.or ...
- 旧电脑做服务器--第一篇 sql server 服务器搭建
背景:旧电脑为2015年的老电脑,联系G50系列,目前键盘鼠标操作都有问题,键盘按键和鼠标左键莫名奇妙变成右击,屏幕显示也是大颗粒.但是配置还可以,16GB内存+256GB三星固态硬盘.所以想搭建作为 ...
- js 正则表达式 验证与限制
.... //验证身份证格式 function onIDCardsValidation(e) { if (e.isValid) { var pattern = /\d*/; if (!(e.value ...
- 菜鸡的Java笔记 第八 - java 面向对象
面向对象的特点以及开发过程. java中最大的特点是其支持面向对象编程设计思想.在面向对象之前广泛流传的是面向过程的编程思想,例如:C语言的开发就属于面向过程 如果要想更简单的去理解面向过 ...
- Python编程环境设置
第1节.Python编程环境设置 一.sublime相关 1.sublime REPL插件安装 (1)安装 先打开插件安装面板:ctrl+shift+P 输入install ,选择Package Co ...
- X-MagicBox-820的luatOS之路连载系列2
这块MagicBox小巧但外设丰富,盖板上的小液晶屏竟有240*240的分辨率.点亮后若是用最小字体,真有看瞎老王的不瞎之眼之势. 这种屏在某宝也是比较多的,大概就是长这样子: 我们这个820的盖板上 ...
- [atAGC048F]01 Record
先将这个序列翻转,贪心找到最长的'101010--'的形式的子序列并删除,重复此过程并记这些字符串长度依次为$l_{1},l_{2},...,l_{n}$,若最终还有字符剩余则一定无解 假设$S$中元 ...
- 【Tool】IntelliJ IDEA Ultimate2019.1 中文版 安装
IntelliJ IDEA Ultimate2019.1 2019-07-26 09:26:24 by冲冲 1. 下载 https://mp.weixin.qq.com/s/SdFQqGzMy-g ...
- 【Microsoft Azure 的1024种玩法】七.Azure云端搭建部署属于自己的维基百科
[简介] MediaWiki是全球最著名的开源wiki程序,运行于PHP+MySQL环境.MediaWiki从2002年2月25日被作为维基百科全书的系统软件,并有大量其他应用实例.MediaWiki ...
- 三个 AGC D(AGC037D、AGC043D、AGC050D)
大概就 lxr 讲了 4 个 AGC 的 D,有一个以前做过了不算,另外三个都会做罢( 为了避免开三个博客就把它们合并到一起了 AGC 037 D lxr:难度顺序排列大概是 037<043&l ...