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

  

最新文章

  1. (转)redis 3.0的集群部署
  2. 项目中应用eventbus解决的问题
  3. java 实现https请求
  4. 【英语】Bingo口语笔记(5) - 英式和美式英语的发音区别
  5. android移植
  6. 使用Intent在活动之间穿梭(《第一行代码》读书笔记)
  7. python -- 函数传参
  8. ajax请求webservice时抛出终止线程的异常
  9. 【转】用户空间使用i2c_dev--不错
  10. Django模板-在视图中使用模板
  11. Hive常用操作之数据导入导出
  12. Twenty Newsgroups Classification任务之二seq2sparse
  13. 在Linux下,在网络没有配置好前,怎样查看网卡的MAC地址?
  14. cocoapods安装好后repo换源
  15. input type file兼容性
  16. windows怎么与虚拟机linux共享
  17. pymongo的用法
  18. hystrix dashboard Unable to connect to Command Metric Stream解决办法
  19. bowtie:短序列比对的新工具
  20. Spherical Hashing,球哈希

热门文章

  1. prometheus(5)之consul服务自动发现及pushgetway
  2. Windows内核中的CPU架构-8-任务段TSS(task state segment)
  3. Python基础(list与tuple)
  4. pyhon-高并发测试
  5. [loj6254]最优卡组
  6. littlevgl架构浅析
  7. Netty高性能网络应用框架对标P7面试题分享v4.1.70.Final
  8. Dapr-可观测性
  9. 描述高频题之队列&amp;栈
  10. Codeforces 1290F - Making Shapes(数位 dp)