C ++ class implements partial closure

First, although often mentioned closures, but I really was not clear on the concept of closure, feeling vaguely defined and if the function A return to the function B, B can still run and function normally outside a function and access function A a defined variables, while variables a function can not be defined in external access, called closure - if this understanding is wrong, then when I did not say anything!

See someone say blogging to achieve closure by new features of C ++ 11 std :: bind, carefully thought for a moment, in fact, by two features of C ++ 03 can be achieved: one local class, is a static local variable.

Static local variables

C ++ allows you to define a static inside a function local variables, which will not be destroyed when leaving the function, like this

  1. void func() { 
  2.     static int i = 0; 
  3.     cout << i++ << endl; 

If you call func many times, you will find the value output to the console has been incremental.

Local class 

Want to define a local function is not possible in the function, C ++ is not the syntax. However, you can define a function in a local class, which can only be used in this function, the very interesting, like this

  1. void func() { 
  2.     class LocalClass { 
  3.     private: 
  4.         // Private members 
  5.     public: 
  6.         void run() { 
  7.             // ...... 
  8.         } 
  9.     } obj; 
  10.     obj.run(); 

If you need a local class object is returned to the outside, it is necessary to define an interface, for receiving an object and a method wherein the return call; while in the new functions required to generate the object and returns the pointer, such as

  1. class ILocal { 
  2. public: 
  3.     virtual void run() = 0; 
  4. }; 
  5.  
  6. ILOCA * func () { 
  7.     class LocalClass: public ILocal { 
  8.     public: 
  9.         virtual void run() { 
  10.             // ...... 
  11.         } 
  12.     }; 
  13.     return new LocalClass(); 

基础知识具备了,下面来实现闭包,过程就不说了,看注释吧

  1. #include <iostream> 
  2. using namespace std; 
  3.  
  4. //////// 接口法实现 //////////////////////////////////////// 
  5. // 定义一个函数对象接口……就像C#的委托 
  6. class ITest { 
  7. public: 
  8.     // 定义这个运算符主要是为了像函数一样调用,就像这样:obj(); 
  9.     void operator() () { 
  10.         process(); 
  11.     } 
  12.  
  13. protected: 
  14.     // 接口函数 
  15.     virtual void process() = 0; 
  16. }; 
  17.  
  18. // 下面函数返回一个ITest对象指针 
  19. ITest* test() { 
  20.     // 函数内的静态变量,离开函数也不会销毁,而且可以 
  21.     static int count = 0; 
  22.  
  23.     // 定义一个局部类 
  24.     class Test: public ITest { 
  25.     public: 
  26.         // 实现ITest中定义的接口函数来实现操作 
  27.         virtual void process() { 
  28.             cout << "Count is " << count++ << endl; 
  29.         } 
  30.     }; 
  31.  
  32.     // 返回一个新的对象……这里必须得new,你懂的 
  33.     return new Test(); 
  34.  
  35. //////// 函数法实现 //////////////////////////////////////// 
  36. // 定义测试函数指针类型 
  37. typedef void (*Func)(const char*); 
  38.  
  39. // 下面函数返回一个闭包中的函数 
  40. Func testFunc() { 
  41.     // 静态局部变量初始化为100 
  42.     static int count = 100; 
  43.     // 静态局部常量 
  44.     static const char* const NAME = "James"; 
  45.  
  46.     // 不能直接定义函数,只好定义局部类的静态方法,并返回其指针 
  47.     class Test { 
  48.     public: 
  49.         // 这个定义说明可以传入参数,同理也可以有返回值 
  50.         static void process(const char* pName = NULL) { 
  51.             if (pName == NULL) { pName = NAME; } 
  52.             cout << pName << " Count is " << count-- << endl; 
  53.         } 
  54.     }; 
  55.     return Test::process; 
  56.  
  57. //////// 程序入口:主函数 ////////////////////////////////// 
  58. int main(int argc, char* argv[]) { 
  59.     ITest* pT = test(); 
  60.     Func pF = testFunc(); 
  61.  
  62.     // 多次调用得从函数里返回出来的函数和对象,观察结果 
  63.     for (int i = 0; i < 10; i++) { 
  64.         (*pT)(); 
  65.         pF((i % 2 == 0) ? NULL : "Fancy"); 
  66.     } 

给个运行结果

  1. Count is 0 
  2. James Count is 100 
  3. Count is 1 
  4. Fancy Count is 99 
  5. Count is 2 
  6. James Count is 98 
  7. Count is 3 
  8. Fancy Count is 97 
  9. Count is 4 
  10. James Count is 96 
  11. Count is 5 
  12. Fancy Count is 95 
  13. Count is 6 
  14. James Count is 94 
  15. Count is 7 
  16. Fancy Count is 93 
  17. Count is 8 
  18. James Count is 92 
  19. Count is 9 
  20. Fancy Count is 91 

C++是门非常灵活的语言,我们平时用到的相对于C++ Specification来说,仅仅是冰山一角。虽然因为工作关系我已经有四五年没用它了,但它依然是我最喜欢的语言之一。

 

1、物理学(第六版)上册、下册 马文蔚 周玉青 版 课后习题答案 大学通用物理 课后题答案 东南大学等七所工科院校汇编 课后习题答案与解析 课后答案 全集https://www.cnblogs.com/liudianjia/p/12535157.html
2、编程精品教材:MATLAB程序设计与应用(第3版) 课后答案 刘卫国版 课后习题答案解析https://www.cnblogs.com/liudianjia/p/12513320.html
3、C语言程序设计(第3版) 何钦铭 颜晖 版 课后答案 习题解析https://www.cnblogs.com/liudianjia/p/12513272.html
4、数据库系统概论(第5版)课后答案https://www.cnblogs.com/liudianjia/p/12495990.html
5、数学物理方程第二版 课后习题答案 季孝达 薛兴恒 陆英 宋立功 版 第1章 课后答案解析https://www.cnblogs.com/liudianjia/p/12488902.html
6、Python语言程序设计基础(第2版)课后习题答案 嵩天、礼欣、黄天羽版 高等教育出版社 试题和答案和解析
https://www.cnblogs.com/liudianjia/p/12484952.html
7、大学 高等数学 大学线性代数 第五版 同济大学版 工程数学线性代数 课后习题答案https://www.cnblogs.com/liudianjia/p/12479110.html
8、Python语言程序设计基础(第2版)课后习题答案 嵩天、礼欣、黄天羽版 高等教育出版社 试题和答案和解析https://www.cnblogs.com/liu2020/p/12515245.html
9、编程精品教材:MATLAB程序设计与应用(第3版) 课后答案 刘卫国版 课后习题答案解析https://www.cnblogs.com/liu2020/p/12515240.html
10、离散数学(第2版)课后习题答案https://www.cnblogs.com/liu2020/p/12496066.html
11、数学物理方程 课后习题答案 清华大学出版社出版 姜玉山、徐延钦、王晓敏、张庆灵、刘超、李明维版 第2章 课后题答案与解析 复习提纲https://www.cnblogs.com/liu2020/p/12489032.html
12、高等数学(第七版)上册、下册 课后习题答案 同济大学数学系编写 高等教育出版社 大学数学 高等数学第七版第三章 答案与解析https://www.cnblogs.com/liu2020/p/12485055.html
13、工程数学线性代数 同济大学版 第六版 课后习题答案 高等数学 大学数学 线性代数 课后题答案与解析 考试复习提纲https://www.cnblogs.com/liu2020/p/12481993.html
14、数学模型(第五版)习题参考解答 高清 PDF电子版 电子书 电子版读书 姜启源、谢金星、叶俊 版 习题答案与解析https://www.cnblogs.com/awzh2020/p/12541824.html
15、操作系统教程(第5版)费翔林 骆斌 版 课后答案 第2章 课后习题答案解析https://www.cnblogs.com/awzh2020/p/12513557.html
16、离散数学学习指导与习题解析(第2版)PDF电子版图书 屈婉玲 耿素云 张立昂 课后题答案https://www.cnblogs.com/awzh2020/p/12513483.html

Guess you like

Origin www.cnblogs.com/xiangcunjiaoshi/p/12566180.html
Recommended