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++的更多相关文章

  1. Java - Nested Classes

    (本文参考:http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html) Nested Classes class OuterClas ...

  2. Java Nested Classes(内部类~第一篇英文技术文档翻译)

    鄙人最近尝试着翻译了自己的第一篇英文技术文档.Java Nested Classes Reference From Oracle Documentation 目录 嵌套类-Nested Classes ...

  3. Nested Classes

    http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html package priceton; /* * Copyright (c) ...

  4. Java中的Nested Classes和Inner Classes

    Java中的Nested Classes和Inner Classes Java有嵌套类(Nested Classes)和内部类(Inner Classes)的概念. 嵌套类(Nested Classe ...

  5. 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 ...

  6. Effetive Java 22 Favor static member classes over nonstatic

    Nested class types Usage and remark Advantage Disadvantage static member classes Use for public help ...

  7. Visual C++ 里的 Classes, Methods and RTTI

    类的基本布局 为了说明以下内容,让我们考虑这个简单的例子: class A { int a1; public: virtual int A_virt1(); virtual int A_virt2() ...

  8. 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 ...

  9. .NET平台开源项目速览(18)C#平台JSON实体类生成器JSON C# Class Generator

    去年,我在一篇文章用原始方法解析复杂字符串,json一定要用JsonMapper么?中介绍了简单的JSON解析的问题,那种方法在当时的环境是非常方便的,因为不需要生成实体类,结构很容易解析.但随着业务 ...

随机推荐

  1. 在 macOS 上运行无限许可的 Nessus 10

    请访问原文链接:https://sysin.org/blog/nessus-unlimited-on-macos/,查看最新版.原创作品,转载请保留出处. 作者:gc(at)sysin.org,主页: ...

  2. OpenYurt 与 FabEdge 集成验证——云边数据面通信初试

    作者|浙江大学 SEL 实验室:晋晨.博云:耿浩涛 审核&校对:海珠 编辑&排版:雯燕 背景 在近几年的产业环境下,传统云计算能力已无法支撑起规模日趋庞大且异地分散的数据处理与计算需求 ...

  3. SpringCloud升级之路2020.0.x版-34.验证重试配置正确性(2)

    本系列代码地址:https://github.com/JoJoTec/spring-cloud-parent 我们继续上一节针对我们的重试进行测试 验证针对限流器异常的重试正确 通过系列前面的源码分析 ...

  4. Linux基础四:软件包管理

    四.软件包管理器: 1.概念 红帽有两款软件包管理器,分别是rpm和yum. 1.rpm软件包管理器  ->  用来安装单个包  ->  .rpm文件 红帽的安装包文件,都放在Packag ...

  5. [cf878D]Magic Breeding

    对于每一行,用一个2^12个01来表示,其中这一行就是其中所有为1的点所代表的行(i二进制中包含的行)的max的min,然后就可以支持取max和min了,查询只需要枚举答案即可 1 #include& ...

  6. JavaScript高级程序设计读后感(一)之零碎知识点查漏补缺

    目录 1-script延迟脚本defer及异步脚本async,区别及应用场景 2-未声明的变量,未初始化变量 3-Number parseInt 字符串转数值 ,进制转换 4-undefined &a ...

  7. 从零开始学Kotlin第五课

    函数式编程入门: package EL fun main(args: Array<String>) { var names= listOf<String>("tom& ...

  8. 打开order by的大门,一探究竟《死磕MySQL系列 十二》

    在日常开发工作中,你一定会经常遇到要根据指定字段进行排序的需求. 这时,你的SQL语句类似这样. select id,phone,code from evt_sms where phone like  ...

  9. SpringCloud升级之路2020.0.x版-43.为何 SpringCloudGateway 中会有链路信息丢失

    本系列代码地址:https://github.com/JoJoTec/spring-cloud-parent 在开始编写我们自己的日志 Filter 之前,还有一个问题我想在这里和大家分享,即在 Sp ...

  10. R语言与医学统计图形【8】颜色的选取

    R语言基础绘图系统 基础绘图包之低级绘图函数--内置颜色. 1.内置颜色选取 功能657种内置颜色.colors() 调色板函数:palette(), rgb(), rainbow(). palett ...