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
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
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
|
基础知识具备了,下面来实现闭包,过程就不说了,看注释吧
- #include <iostream>
- using namespace std;
- //////// 接口法实现 ////////////////////////////////////////
- // 定义一个函数对象接口……就像C#的委托
- class ITest {
- public:
- // 定义这个运算符主要是为了像函数一样调用,就像这样:obj();
- void operator() () {
- process();
- }
- protected:
- // 接口函数
- virtual void process() = 0;
- };
- // 下面函数返回一个ITest对象指针
- ITest* test() {
- // 函数内的静态变量,离开函数也不会销毁,而且可以
- static int count = 0;
- // 定义一个局部类
- class Test: public ITest {
- public:
- // 实现ITest中定义的接口函数来实现操作
- virtual void process() {
- cout << "Count is " << count++ << endl;
- }
- };
- // 返回一个新的对象……这里必须得new,你懂的
- return new Test();
- }
- //////// 函数法实现 ////////////////////////////////////////
- // 定义测试函数指针类型
- typedef void (*Func)(const char*);
- // 下面函数返回一个闭包中的函数
- Func testFunc() {
- // 静态局部变量初始化为100
- static int count = 100;
- // 静态局部常量
- static const char* const NAME = "James";
- // 不能直接定义函数,只好定义局部类的静态方法,并返回其指针
- class Test {
- public:
- // 这个定义说明可以传入参数,同理也可以有返回值
- static void process(const char* pName = NULL) {
- if (pName == NULL) { pName = NAME; }
- cout << pName << " Count is " << count-- << endl;
- }
- };
- return Test::process;
- }
- //////// 程序入口:主函数 //////////////////////////////////
- int main(int argc, char* argv[]) {
- ITest* pT = test();
- Func pF = testFunc();
- // 多次调用得从函数里返回出来的函数和对象,观察结果
- for (int i = 0; i < 10; i++) {
- (*pT)();
- pF((i % 2 == 0) ? NULL : "Fancy");
- }
- }
给个运行结果
- Count is 0
- James Count is 100
- Count is 1
- Fancy Count is 99
- Count is 2
- James Count is 98
- Count is 3
- Fancy Count is 97
- Count is 4
- James Count is 96
- Count is 5
- Fancy Count is 95
- Count is 6
- James Count is 94
- Count is 7
- Fancy Count is 93
- Count is 8
- James Count is 92
- Count is 9
- 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