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解析的问题,那种方法在当时的环境是非常方便的,因为不需要生成实体类,结构很容易解析.但随着业务 ...
随机推荐
- 【java+selenium3】模拟键盘操作 (十二)
一.键盘操作 用代码来模拟键盘的Enter或一系列的组合键,前面使用sendkeys()方法模拟键盘的输入,除此之外还可以模拟键盘组合键输入如下: 整理一些比较常用的键盘操作如下: sendKeys( ...
- 4. 理解Update、Enter、Exit 与 添加、删除元素
理解Update.Enter.Exit 与 添加.删除元素 在使用data()绑定数据时,例如:现在我们有一个数组[3,6,9,12,15],我们可以将数组每一项与一个<p>绑定,但是,现 ...
- [linux]centos7.4上升级python2版本到python3.6.5 【安装双版本,默认python3】
版本声明 centos7.4 前言:linux上的python默认是版本2的,之前学django项目用的是3的版本 所以得升级下版本~ 1.下载python3.6.5 cd /usr/local/ w ...
- linux 入门系列-基础性知识
1:初探linux-基于centos7 运维和服务器硬件组合 两种登录方式:(1)-------root:管理员登录权限较高,不建议初学者使用格式: [root@centos7 jinlong]# ( ...
- Redis这15个“雷坑”,别问我咋知道的……
来源:https://dbaplus.cn/news-158-3836-1.html
- yum设置取消代理
unset http_proxy unset https_proxy 暂时取消代理,若永久取消代理,需要修改/etc/yum.conf 去掉 proxy=http://proxyhost:8080
- SpringCloud升级之路2020.0.x版-32. 改进负载均衡算法
本系列代码地址:https://github.com/JoJoTec/spring-cloud-parent 在前面一节,我们梳理了实现 Feign 断路器以及线程隔离的思路,这一节,我们先不看如何源 ...
- Mac 安装 RabbitMQ 出现的问题
安装 RabbitMQ 在官网这里按照步骤来就行 出现Error: Permission denied @ apply2files - /usr/local/lib/docker/cli-plugin ...
- 数据库学习笔记 - MySQL基础知识
一.数据库基础知识 1.1 Whats's 数据库 数据库(Database,DB):将大量数据保存起来,通过计算机加工而成的可以进行高效访问的数据集合.如:大型-银行存储的信息,小型-电话簿. 数据 ...
- 性能优化 | Go Ballast 让内存控制更加丝滑
关于 Go GC 优化的手段你知道的有哪些?比较常见的是通过调整 GC 的步调,以调整 GC 的触发频率. 设置 GOGC 设置 debug.SetGCPercent() 这两种方式的原理和效果都是一 ...