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. 1 #include<iostream>
  2. 2 using namespace std;
  3. 3
  4. 4 void fun()
  5. 5 {
  6. 6 class Test // local to fun
  7. 7 {
  8. 8 /* members of Test class */
  9. 9 };
  10. 10 }
  11. 11
  12. 12 int main()
  13. 13 {
  14. 14 return 0;
  15. 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. 1 #include<iostream>
  2. 2 using namespace std;
  3. 3
  4. 4 void fun()
  5. 5 {
  6. 6 // Local class
  7. 7 class Test
  8. 8 {
  9. 9 /* ... */
  10. 10 };
  11. 11
  12. 12 Test t; // Fine
  13. 13 Test *tp; // Fine
  14. 14 }
  15. 15
  16. 16 int main()
  17. 17 {
  18. 18 Test t; // Error
  19. 19 Test *tp; // Error
  20. 20 return 0;
  21. 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. 1 // PROGRAM 1
  2. 2 #include<iostream>
  3. 3 using namespace std;
  4. 4
  5. 5 void fun()
  6. 6 {
  7. 7 class Test // local to fun
  8. 8 {
  9. 9 public:
  10. 10 // Fine as the method is defined inside the local class
  11. 11 void method()
  12. 12 {
  13. 13 cout << "Local Class method() called";
  14. 14 }
  15. 15 };
  16. 16
  17. 17 Test t;
  18. 18 t.method();
  19. 19 }
  20. 20
  21. 21 int main()
  22. 22 {
  23. 23 fun();
  24. 24 return 0;
  25. 25 }

  Output:

  Local Class method() called

  1. 1 // PROGRAM 2
  2. 2 #include<iostream>
  3. 3 using namespace std;
  4. 4
  5. 5 void fun()
  6. 6 {
  7. 7 class Test // local to fun
  8. 8 {
  9. 9 public:
  10. 10 void method();
  11. 11 };
  12. 12
  13. 13 // Error as the method is defined outside the local class
  14. 14 void Test::method()
  15. 15 {
  16. 16 cout << "Local Class method()";
  17. 17 }
  18. 18 }
  19. 19
  20. 20 int main()
  21. 21 {
  22. 22 return 0;
  23. 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. 1 // PROGRAM 1
  2. 2 #include<iostream>
  3. 3 using namespace std;
  4. 4
  5. 5 void fun()
  6. 6 {
  7. 7 class Test // local to fun
  8. 8 {
  9. 9 static int i;
  10. 10 };
  11. 11 }
  12. 12
  13. 13 int main()
  14. 14 {
  15. 15 return 0;
  16. 16 }

  Compiler Error:

  In function 'void fun()':  error: local class 'class fun()::Test' shall not have static data member 'int fun()::Test::i'

  1. 1 // PROGRAM 2
  2. 2 #include<iostream>
  3. 3 using namespace std;
  4. 4
  5. 5 void fun()
  6. 6 {
  7. 7 class Test // local to fun
  8. 8 {
  9. 9 public:
  10. 10 static void method()
  11. 11 {
  12. 12 cout << "Local Class method() called";
  13. 13 }
  14. 14 };
  15. 15
  16. 16 Test::method();
  17. 17 }
  18. 18
  19. 19 int main()
  20. 20 {
  21. 21 fun();
  22. 22 return 0;
  23. 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. 1 // PROGRAM 1
  2. 2 #include<iostream>
  3. 3 using namespace std;
  4. 4
  5. 5 void fun()
  6. 6 {
  7. 7 static int x;
  8. 8 enum {i = 1, j = 2};
  9. 9
  10. 10 // Local class
  11. 11 class Test
  12. 12 {
  13. 13 public:
  14. 14 void method()
  15. 15 {
  16. 16 cout << "x = " << x << endl; // fine as x is static
  17. 17 cout << "i = " << i << endl; // fine as i is enum
  18. 18 }
  19. 19 };
  20. 20
  21. 21 Test t;
  22. 22 t.method();
  23. 23 }
  24. 24
  25. 25 int main()
  26. 26 {
  27. 27 fun();
  28. 28 return 0;
  29. 29 }

  Output:

  x = 0
  i = 1

  1. 1 // PROGRAM 2
  2. 2 #include<iostream>
  3. 3 using namespace std;
  4. 4
  5. 5 void fun()
  6. 6 {
  7. 7 int x;
  8. 8
  9. 9 // Local class
  10. 10 class Test
  11. 11 {
  12. 12 public:
  13. 13 void method()
  14. 14 {
  15. 15 cout << "x = " << x << endl;
  16. 16 }
  17. 17 };
  18. 18
  19. 19 Test t;
  20. 20 t.method();
  21. 21 }
  22. 22
  23. 23 int main()
  24. 24 {
  25. 25 fun();
  26. 26 return 0;
  27. 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. 1 #include<iostream>
  2. 2 using namespace std;
  3. 3
  4. 4 int x;
  5. 5
  6. 6 void fun()
  7. 7 {
  8. 8
  9. 9 // First Local class
  10. 10 class Test1
  11. 11 {
  12. 12 public:
  13. 13 Test1()
  14. 14 {
  15. 15 cout << "Test1::Test1()" << endl;
  16. 16 }
  17. 17 };
  18. 18
  19. 19 // Second Local class
  20. 20 class Test2
  21. 21 {
  22. 22 // Fine: A local class can use other local classes of same function
  23. 23 Test1 t1;
  24. 24 public:
  25. 25 void method()
  26. 26 {
  27. 27 // Fine: Local class member methods can access global variables.
  28. 28 cout << "x = " << x << endl;
  29. 29 }
  30. 30 };
  31. 31
  32. 32 Test2 t;
  33. 33 t.method();
  34. 34 }
  35. 35
  36. 36 int main()
  37. 37 {
  38. 38 fun();
  39. 39 return 0;
  40. 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++的更多相关文章

  1. Java Inner Classes

    When thinking about inner classes in java, the first thing that comes to my mind is that, WHY do we ...

  2. Effetive Java 22 Favor static member classes over nonstatic

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

  3. Java - Nested Classes

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

  4. JAVA匿名内部类(Anonymous Classes)

    1.前言 匿名内部类在我们JAVA程序员的日常工作中经常要用到,但是很多时候也只是照本宣科地用,虽然也在用,但往往忽略了以下几点:为什么能这么用?匿名内部类的语法是怎样的?有哪些限制?因此,最近,我在 ...

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

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

  6. Window Classes in Win32

    探索Win32系统之窗口类(Window Classes in Win32) Kyle MarshMicrosoft Developer Network Technology GroupMSDN技术组 ...

  7. Java

    2016-12-17  21:10:28 吉祥物:Duke(公爵)    Logo:咖啡(爪哇岛盛产咖啡)  An overview of the software development proce ...

  8. 让VisualVM+BTrace进入unsafe mode

    让VisualVM+BTrace进入unsafe mode http://kenai.com/projects/btrace/pages/UserGuide BTrace很强大,但有很多安全限制,比如 ...

  9. 探索Win32系统之窗口类(转载)

    Window Classes in Win32 摘要 本文主要介绍win32系统里窗口类的运做和使用机制,探索一些细节问题,使win32窗口类的信息更加明朗化. 在本文中,"类", ...

随机推荐

  1. Jmeter二次开发实现自定义functions函数(九)

    在Jmeter->选项->函数助手对话框中我们可以看到Jmeter内置的一些常用函数,但考虑到测试过程中的实际情况,我们经常需要在脚本引用或者实现自定义的函数.那么如何在"函数助 ...

  2. java+selenium+testNG+Allure报表【新增截图到报表功能】

    1.pom.xml配置 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://w ...

  3. 使用silky脚手架构建微服务应用

    目录 模板简介 构建独立应用的模板Silky.App.Template 构建模块化应用的模板Silky.Module.Template 开源地址 在线文档 模板简介 使用 dotnet new 命令可 ...

  4. [linux]centos7.4上升级python2版本到python3.6.5 【安装双版本,默认python3】

    版本声明 centos7.4 前言:linux上的python默认是版本2的,之前学django项目用的是3的版本 所以得升级下版本~ 1.下载python3.6.5 cd /usr/local/ w ...

  5. java语言方法中定义final类型的入参有什么用意?

    无论参数是基本数据类型,还是引用数据类型,只要加了final,不好意思,该参数不可以再赋值(实参传进来给形参,就相当于初始化完成).可以防止在方法里面不小心重新赋值,造成一些不必要的麻烦!!!参考:h ...

  6. java eclipse调试提示Source not found 或 一闪而过 解决方法

    Web工程Eclipse  debug方式启动,在断点的位置被成功拦截,但是没有跳转到工程的代码处,提示如下: 当然这个时候如果我继续按F5的话呢,程序又会接着正常运行了.到这里那就是说程序本身是没有 ...

  7. Linux基础三:用户和组

    三.用户和组 1.概念 (1).用户概念: 用户是用来运行某一些进程.拥有某一些文件或目录. 在Linux里面,用户分成三大类:root用户.系统用户.普通用户. 用户是用UID来唯一标识身份的,且r ...

  8. ECharts 点击事件

    一个问题 ECharts 点击出现多个弹窗

  9. [loj2245]魔法森林

    枚举携带的"A型守护精灵"数$A_{0}$,那么即只能经过$A_{i}\le A_{0}$的边,并最小化1到$n$路径上最大的$B_{i}$ 将所有边按照$A_{i}$从小到大排序 ...

  10. [loj2135]幻想乡战略游戏

    以1为根建树,令$D_{i}$为$i$子树内所有节点$d_{i}$之和 令$ans_{i}$为节点$i$的答案,令$fa$为$i$的父亲,则$ans_{i}=ans_{fa}+dis(i,fa)(D_ ...