C/C++ 不带参数的回调函数 与 带参数的回调函数 函数指针数组 例子
先来不带参数的回调函数例子
#include <iostream>
#include <windows.h> void printFunc()
{
std::cout<<"printFunc"<<std::endl;
} void CallFunc(void (*FuncPoint)())
{
FuncPoint();
} int main(int argc,char* argv[])
{
CallFunc(printFunc);
system("pause");
return 0;
}
然后带参数的回调函数
#include <iostream>
#include <windows.h>
void printFunc(int a)
{
std::cout<<"printFunc : "<<a<<std::endl;
}
void CallFunc(void (*FuncPoint)(int),int a)
{
FuncPoint(a);
} int main(int argc,char* argv[])
{
CallFunc(printFunc,123); system("pause");
return 0;
}
函数指针数组的使用
#include <iostream>
#include <windows.h> void printFunc1(int a)
{
std::cout<<"printFunc1 : "<<a<<std::endl;
} void printFunc2(int a)
{
std::cout<<"printFunc2 : "<<a<<std::endl;
} int main(int argc,char* argv[])
{ void (*funcPointArray[2])(int)={printFunc1,printFunc2}; for (int i=0;i<2;i++)
{
funcPointArray[i](3);
}
system("pause");
return 0;
}
使用TypeDef来简化上面的代码
#include <iostream>
#include <windows.h> typedef void (*FuncPoint)(int); void printFunc1(int a)
{
std::cout<<"printFunc1 : "<<a<<std::endl;
} void printFunc2(int a)
{
std::cout<<"printFunc2 : "<<a<<std::endl;
} int main(int argc,char* argv[])
{ //void (*funcPointArray[2])(int)={printFunc1,printFunc2}; FuncPoint funcPointArray[]={printFunc1,printFunc2}; for (int i=0;i<2;i++)
{
funcPointArray[i](3);
}
system("pause");
return 0;
}
本来我们需要使用:
void (*funcPointArray[2])(int)
其中变量是
funcPointArray[2]
于是 在TypeDef 里 我们用 FuncPoint 来代替这个变量,代替了上面整的一句话。
typedef void (*FuncPoint)(int);
后面使用的时候就用 FuncPoint
FuncPoint funcPointArray[]={printFunc1,printFunc2};
C/C++ 不带参数的回调函数 与 带参数的回调函数 函数指针数组 例子的更多相关文章
- c/c++ main 函数命令行参数的使用
C程序最大的特点就是所有的程序都是用函数来装配的.main()称之为主函数,是所有程 序运行的入口.其余函数分为有参或无参两种,均由main()函数或其它一般函数调用,若调用的是有参函数,则参数在调用 ...
- 函数指针的返回值是指针数组,数组里放的是int;函数指针的返回值是指针数组,数组里放的是int指针
函数指针的返回值是指针数组,数组里放的是int 函数指针的返回值是指针数组,数组里放的是int指针 #include <stdio.h> #include <stdlib.h> ...
- PHP之回调函数传参(解决eval函数拼接对象参数的问题)
在使用Smarty时,定义了一个统一调用控制器的函数,如下: function C($name, $method){//控制器的名称和其中方法的名称 require_once "contro ...
- 关于C#中函数声明带参数的函数
在C#语言的函数中,有一项至关重要的我们称之为参数. 对于参数的含义:要完成一件事,需要知道的额外条件 其语法: static void 函数名(参数列表){ //注释类容} 而其参数列表的语法为: ...
- python 全栈开发,Day12(函数的有用信息,带参数的装饰器,多个装饰器装饰一个函数)
函数的执行时,*打散.函数的定义时,*聚合. from functools import wraps def wrapper(f): # f = func1 @wraps(f) def inner(* ...
- promise对象的回调函数resolve的参数为另一个promise对象
/*如果调用resolve函数和reject函数时带有参数,那么它们的参数会被传递给回调函数. reject函数的参数通常是Error对象的实例,表示抛出的错误: resolve函数的参数除了正常的值 ...
- 【python 】装饰器 (多个参数的函数,带参数的装饰器)【转】
最简单的模板是这样的 #-*-coding:utf-8-*- def outer(func): def inner(): print 'before' func() print 'after' # r ...
- python 装饰器 (多个参数的函数,带参数的装饰器)
最简单的模板是这样的 #-*-coding:utf-8-*- def outer(func): def inner(): print 'before' func() print 'after' # r ...
- 快学Scala 第二课 (apply, if表达式,循环,函数的带名参数,可变长参数,异常)
apply方法是Scala中十分常见的方法,你可以把这种用法当做是()操作符的重载形式. 像以上这样伴生对象的apply方法是Scala中构建对象的常用手法,不再需要使用new. if 条件表达式的值 ...
随机推荐
- bzoj2432
被虐的体无完肤, 直接给题解地址吧:http://vfleaking.blog.163.com/blog/static/174807634201341721051604/ ; ..,..] of in ...
- css配合js模拟的select下拉框
css配合js模拟的select下拉框 <!doctype html> <html> <head> <meta charset="utf-8&quo ...
- css动画集合地址
CSS3 UI Lib库是由腾讯AlloyTeam前端开发团队建立,主要收集国内外友好体验和创意的界面组件Demo. 它除了使用CSS3技术外,还使用了HTML5,JS,JX,jQuery等技术,来达 ...
- 转载:Unobtrusive JavaScript in ASP.NET MVC 3 隐式的脚本在MVC3
Unobtrusive JavaScript 是什么? <!--以下是常规Javascript下写出来的Ajax--> <div id="test"> &l ...
- busybox filesystem ts_config: No such file or directory
/******************************************************************** * busybox filesystem ts_config ...
- POJ 3177 Redundant Paths (桥,边双连通分量,有重边)
题意:给一个无向图,问需要补多少条边才可以让整个图变成[边双连通图],即任意两个点对之间的一条路径全垮掉,这两个点对仍可以通过其他路径而互通. 思路:POJ 3352的升级版,听说这个图会给重边.先看 ...
- Java [Leetcode 70]Climbing Stairs
题目描述: You are climbing a stair case. It takes n steps to reach to the top. Each time you can either ...
- LeetCode: Combination Sum I && II && III
Title: https://leetcode.com/problems/combination-sum/ Given a set of candidate numbers (C) and a tar ...
- 【转】AVL
#include <iostream> #include <ctime> #include <queue> #include <cassert> #in ...
- Hadoop中的InputFormat解析
1.InputFormat InputFormat是Hadoop平台上Mapreduce输入的规范,仅有两个抽象方法. List<InputSplit> getSplits(), 获取由输 ...