先来不带参数的回调函数例子

#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++ 不带参数的回调函数 与 带参数的回调函数 函数指针数组 例子的更多相关文章

  1. c/c++ main 函数命令行参数的使用

    C程序最大的特点就是所有的程序都是用函数来装配的.main()称之为主函数,是所有程 序运行的入口.其余函数分为有参或无参两种,均由main()函数或其它一般函数调用,若调用的是有参函数,则参数在调用 ...

  2. 函数指针的返回值是指针数组,数组里放的是int;函数指针的返回值是指针数组,数组里放的是int指针

    函数指针的返回值是指针数组,数组里放的是int 函数指针的返回值是指针数组,数组里放的是int指针 #include <stdio.h> #include <stdlib.h> ...

  3. PHP之回调函数传参(解决eval函数拼接对象参数的问题)

    在使用Smarty时,定义了一个统一调用控制器的函数,如下: function C($name, $method){//控制器的名称和其中方法的名称 require_once "contro ...

  4. 关于C#中函数声明带参数的函数

    在C#语言的函数中,有一项至关重要的我们称之为参数. 对于参数的含义:要完成一件事,需要知道的额外条件 其语法: static void 函数名(参数列表){ //注释类容} 而其参数列表的语法为: ...

  5. python 全栈开发,Day12(函数的有用信息,带参数的装饰器,多个装饰器装饰一个函数)

    函数的执行时,*打散.函数的定义时,*聚合. from functools import wraps def wrapper(f): # f = func1 @wraps(f) def inner(* ...

  6. promise对象的回调函数resolve的参数为另一个promise对象

    /*如果调用resolve函数和reject函数时带有参数,那么它们的参数会被传递给回调函数. reject函数的参数通常是Error对象的实例,表示抛出的错误: resolve函数的参数除了正常的值 ...

  7. 【python 】装饰器 (多个参数的函数,带参数的装饰器)【转】

    最简单的模板是这样的 #-*-coding:utf-8-*- def outer(func): def inner(): print 'before' func() print 'after' # r ...

  8. python 装饰器 (多个参数的函数,带参数的装饰器)

    最简单的模板是这样的 #-*-coding:utf-8-*- def outer(func): def inner(): print 'before' func() print 'after' # r ...

  9. 快学Scala 第二课 (apply, if表达式,循环,函数的带名参数,可变长参数,异常)

    apply方法是Scala中十分常见的方法,你可以把这种用法当做是()操作符的重载形式. 像以上这样伴生对象的apply方法是Scala中构建对象的常用手法,不再需要使用new. if 条件表达式的值 ...

随机推荐

  1. 学习Hadoop不错的系列文章

    1)Hadoop学习总结 (1)HDFS简介 (2)HDFS读写过程解析 (3)Map-Reduce入门 (4)Map-Reduce的过程解析 (5)Hadoop的运行痕迹 (6)Apache Had ...

  2. 10 Useful du (Disk Usage) Commands to Find Disk Usage of Files and Directories

    The Linux “du” (Disk Usage) is a standard Unix/Linux command, used to check the information of disk ...

  3. poj 1151 Atlantis (离散化 + 扫描线 + 线段树 矩形面积并)

    题目链接题意:给定n个矩形,求面积并,分别给矩形左上角的坐标和右上角的坐标. 分析: 映射到y轴,并且记录下每个的y坐标,并对y坐标进行离散. 然后按照x从左向右扫描. #include <io ...

  4. CodeForces Round #278 (Div.2) (待续)

    A 这么简单的题直接贴代码好了. #include <cstdio> #include <cmath> using namespace std; bool islucky(in ...

  5. wince和window mobile winphone

    windows mobile是微软在2000年左右推出的针对移动平台的操作系统,这个系统一直使用到三年前,微软开始启用metro界面,将windows mobile改名为windows phone. ...

  6. 【多端应用开发系列1.1.1 —— Android:使用新浪API V2】服务器Json数据处理——Json数据概述

    [前白] 一些基础的东西本系列中就不再详述了,争取尽量写些必不可少的技术要点. 由于本系列把Web Service 构建放到了第二部分,Android项目就采用新浪微博API v2作为服务器端. [原 ...

  7. hdu 4690 EBCDIC

    还有什么好说的呢?打表题= = #include<cstdio> #include<cstring> #include<algorithm> #include< ...

  8. OK335xS psplash make-image-header.sh hacking

    /***************************************************************************** * OK335xS psplash mak ...

  9. TS数据结构分析

    1.TS包得数据结构 2. // Transport packet headertypedef struct TS_packet_header{    unsigned sync_byte       ...

  10. 通过Instant Client包来使用SQL*PLUS

    1.首先下载两个程序包: Instant Client Package - Basic(或Instant Client Package - Basic Lite)包 Instant Client Pa ...