Local Classes in C++
A class declared inside a function becomes local to that function and is called Local Class in C++.
For example, in the following program, Test is a local class in fun().
1 #include<iostream>
2 using namespace std;
3
4 void fun()
5 {
6 class Test // local to fun
7 {
8 /* members of Test class */
9 };
10 }
11
12 int main()
13 {
14 return 0;
15 }
Following are some interesting facts about local classes.
(1) A local class type name can only be used in the enclosing function.
For example, in the following program, declarations of t and tp are valid in fun(), but invalid in main().
1 #include<iostream>
2 using namespace std;
3
4 void fun()
5 {
6 // Local class
7 class Test
8 {
9 /* ... */
10 };
11
12 Test t; // Fine
13 Test *tp; // Fine
14 }
15
16 int main()
17 {
18 Test t; // Error
19 Test *tp; // Error
20 return 0;
21 }
(2)All the methods of Local classes must be defined inside the class only.
For example, program 1 works fine and program 2 fails in compilation.
1 // PROGRAM 1
2 #include<iostream>
3 using namespace std;
4
5 void fun()
6 {
7 class Test // local to fun
8 {
9 public:
10 // Fine as the method is defined inside the local class
11 void method()
12 {
13 cout << "Local Class method() called";
14 }
15 };
16
17 Test t;
18 t.method();
19 }
20
21 int main()
22 {
23 fun();
24 return 0;
25 }
Output:
Local Class method() called
1 // PROGRAM 2
2 #include<iostream>
3 using namespace std;
4
5 void fun()
6 {
7 class Test // local to fun
8 {
9 public:
10 void method();
11 };
12
13 // Error as the method is defined outside the local class
14 void Test::method()
15 {
16 cout << "Local Class method()";
17 }
18 }
19
20 int main()
21 {
22 return 0;
23 }
Output:
Compiler Error: In function 'void fun()': error: a function-definition is not allowed here before '{' token
(3)A Local class cannot contain static data members. It may contain static functions though.
For example, program 1 fails in compilation, but program 2 works fine.
1 // PROGRAM 1
2 #include<iostream>
3 using namespace std;
4
5 void fun()
6 {
7 class Test // local to fun
8 {
9 static int i;
10 };
11 }
12
13 int main()
14 {
15 return 0;
16 }
Compiler Error:
In function 'void fun()': error: local class 'class fun()::Test' shall not have static data member 'int fun()::Test::i'
1 // PROGRAM 2
2 #include<iostream>
3 using namespace std;
4
5 void fun()
6 {
7 class Test // local to fun
8 {
9 public:
10 static void method()
11 {
12 cout << "Local Class method() called";
13 }
14 };
15
16 Test::method();
17 }
18
19 int main()
20 {
21 fun();
22 return 0;
23 }
Output:
Local Class method() called
(4)Member methods of local class can only access static and enum variables of the enclosing function. Non-static variables of the enclosing function are not accessible inside local classes.
For example, the program 1 compiles and runs fine. But, program 2 fails in compilation.
1 // PROGRAM 1
2 #include<iostream>
3 using namespace std;
4
5 void fun()
6 {
7 static int x;
8 enum {i = 1, j = 2};
9
10 // Local class
11 class Test
12 {
13 public:
14 void method()
15 {
16 cout << "x = " << x << endl; // fine as x is static
17 cout << "i = " << i << endl; // fine as i is enum
18 }
19 };
20
21 Test t;
22 t.method();
23 }
24
25 int main()
26 {
27 fun();
28 return 0;
29 }
Output:
x = 0
i = 1
1 // PROGRAM 2
2 #include<iostream>
3 using namespace std;
4
5 void fun()
6 {
7 int x;
8
9 // Local class
10 class Test
11 {
12 public:
13 void method()
14 {
15 cout << "x = " << x << endl;
16 }
17 };
18
19 Test t;
20 t.method();
21 }
22
23 int main()
24 {
25 fun();
26 return 0;
27 }
Output:
In member function 'void fun()::Test::method()': error: use of 'auto' variable from containing function
(5)Local classes can access global types, variables and functions. Also, local classes can access other local classes of same function..
For example, following program works fine.
1 #include<iostream>
2 using namespace std;
3
4 int x;
5
6 void fun()
7 {
8
9 // First Local class
10 class Test1
11 {
12 public:
13 Test1()
14 {
15 cout << "Test1::Test1()" << endl;
16 }
17 };
18
19 // Second Local class
20 class Test2
21 {
22 // Fine: A local class can use other local classes of same function
23 Test1 t1;
24 public:
25 void method()
26 {
27 // Fine: Local class member methods can access global variables.
28 cout << "x = " << x << endl;
29 }
30 };
31
32 Test2 t;
33 t.method();
34 }
35
36 int main()
37 {
38 fun();
39 return 0;
40 }
Output:
Test1::Test1()
x = 0
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:33:59
Local Classes in C++的更多相关文章
- Java Inner Classes
When thinking about inner classes in java, the first thing that comes to my mind is that, WHY do we ...
- Effetive Java 22 Favor static member classes over nonstatic
Nested class types Usage and remark Advantage Disadvantage static member classes Use for public help ...
- Java - Nested Classes
(本文参考:http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html) Nested Classes class OuterClas ...
- JAVA匿名内部类(Anonymous Classes)
1.前言 匿名内部类在我们JAVA程序员的日常工作中经常要用到,但是很多时候也只是照本宣科地用,虽然也在用,但往往忽略了以下几点:为什么能这么用?匿名内部类的语法是怎样的?有哪些限制?因此,最近,我在 ...
- Java Nested Classes(内部类~第一篇英文技术文档翻译)
鄙人最近尝试着翻译了自己的第一篇英文技术文档.Java Nested Classes Reference From Oracle Documentation 目录 嵌套类-Nested Classes ...
- Window Classes in Win32
探索Win32系统之窗口类(Window Classes in Win32) Kyle MarshMicrosoft Developer Network Technology GroupMSDN技术组 ...
- Java
2016-12-17 21:10:28 吉祥物:Duke(公爵) Logo:咖啡(爪哇岛盛产咖啡) An overview of the software development proce ...
- 让VisualVM+BTrace进入unsafe mode
让VisualVM+BTrace进入unsafe mode http://kenai.com/projects/btrace/pages/UserGuide BTrace很强大,但有很多安全限制,比如 ...
- 探索Win32系统之窗口类(转载)
Window Classes in Win32 摘要 本文主要介绍win32系统里窗口类的运做和使用机制,探索一些细节问题,使win32窗口类的信息更加明朗化. 在本文中,"类", ...
随机推荐
- httprunner3源码解读(1)简单介绍源码模块内容
前言 最近想着搭建一个API测试平台,基础的注册登录功能已经完成,就差测试框架的选型,最后还是选择了httprunner,github上已经有很多开源的httprunner测试平台,但是看了下都是基于 ...
- List of devices attached 没有手机设备号 解决办法
问题: cmd下使用adb devices 没有找到手机设备,如下图 解决办法: 采用360手机助手给我们自动安装对应的手机驱动,或者去对应的手机官网下载对应的驱动,这里手机要开启调试模式 我是opp ...
- sklearn模型保存与加载
sklearn模型保存与加载 sklearn模型的保存和加载API 线性回归的模型保存加载案例 保存模型 sklearn模型的保存和加载API from sklearn.externals impor ...
- 记录线上APP一个排序比较引发的崩溃 Comparison method violates its general contract!
最近在做产品需求的时候上线了一个新的产品需求,给用户多了一种新的排序排序规则,更加方便用户找到自己想要的东西.新版本发布后,QA 给我发了一个 线上崩溃 bug 链接,具体内容如下: 看到上面的链接, ...
- Mui中mui.openWindow()方法具体参数信息(内容来自Mui问题专区)
mui.openWindow({ url: 'xxx.html', //String类型,要打开的界面的地址 id: 'id', //String类型,要打开的界面的id styles: { //We ...
- 菜鸡的Java笔记 - java 枚举
枚举 枚举属于加强版的多例设计模式 多例设计模式与枚举 多例设计模式的本质在于构造方法的私有化.而后在类的内部产生若干个实例化对象,随后利用一个 st ...
- 【linux系统】命令学习(八)bash 编程实战学习
常见shell : bash sh zsh windows: git bash cygwin MAC : terminal iterm netstat 是linux下用于显示网络状态的命令.通 ...
- Maven 依赖调解源码解析(二):如何调试 Maven 源码和插件源码
本文是系列文章<Maven 源码解析:依赖调解是如何实现的?>第二篇,主要介绍如何调试 Maven 源码和插件源码.系列文章总目录参见:https://www.cnblogs.com/xi ...
- [bzoj1189]紧急疏散
二分答案+判定,对于一个答案,源点向每一个点连一条流量为1的边,每一扇门向汇点连一条流量为时间的边,每一个人向每一个在答案时间内能走到的门连一条流量为1的边,跑最大流并判断流量是否等于人数. 然而自从 ...
- JAVA后端方面,如何快速达到能实习的程度
概要地讲,是先广度再深度,面试开发两手抓. 首先说学习方法,因为很多初学者没继续下去,不是能力不行,而是方法不当.对比下错误和正确的方法. 1 光看视频光看资料不动手连,这样转眼就忘.正确的做 ...