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. linux网络编程 IO多路复用 select epoll

    本文以我的小型聊天室为例,对于服务器端的代码,做了三次改进,我将分别介绍阻塞式IO,select,epoll . 一:阻塞式IO 对于聊天室这种程序,我们最容易想到的是在服务器端accept之后,然后 ...

  2. Cannot load module file xxx.iml的两种解决方法

    一. 一种是点击左上角File,然后点击Invalidate Caches / Restart...,弹出对话框再点击Invalidate and Restart等待工程重新加载,问题就解决了. 二. ...

  3. 查看Git提交的代码统计

    1,提交Top5: git log --pretty='%aN' | sort | uniq -c | sort -k1 -n -r | head -n 5 2,某用户提交的代码统计 git log ...

  4. vm扩展磁盘容量后不能启动

    主要原因是,新添加的磁盘空间没有分配,系统识别不出来,导致不能开机. 解决方法: 找到虚拟机的文件路径地址,默认是C:\Users\用户名\Documents\Virtual Machines\Cen ...

  5. 高德地图API中折线polyline不能跨越180度经度线的解决方案

    1.问题 最近在使用高德地图的API,有一个需求是画出对象的历史轨迹,采用了高德地图API中的折线polyline函数.但如果需要跨180度经度线的折线,会出现不能跨越的情况,如下图所示: 图中有三个 ...

  6. ndarray 数组的创建和变换

    ndarray数组的创建方法 1.从python中的列表,元组等类型创建ndarray数组 x = np.array(list/tuple) x = np.array(list/tuple,dtype ...

  7. [bzoj4003]城市攻占

    倍增,对于每一个点计算他走到$2^i$次祖先所需要的攻击力以及最终会变成什么(一个一次函数),简单处理即可(然而这样是错的,因为他只保证了骑士的攻击力可以存,并没有保证这个一次函数的系数可以存)(其实 ...

  8. Hi3516开发笔记(四):Hi3516虚拟机编译uboot、kernel、roofts和userdata以及分区表

    若该文为原创文章,转载请注明原文出处本文章博客地址:https://hpzwl.blog.csdn.net/article/details/121572767红胖子(红模仿)的博文大全:开发技术集合( ...

  9. java 代理模式实现代码

    目录 1.静态代理 2.动态代理 1.静态代理 接口类AdminService.java接口 public interface AdminService { void update(); Object ...

  10. JSOI 2008 最小生成树计数

    JSOI 2008 最小生成树计数 今天的题目终于良心一点辣 一个套路+模版题. 考虑昨天讲的那几个结论,我们有当我们只保留最小生成树中权值不超过 $ k $ 的边的时候形成的联通块是一定的. 我们可 ...