C 和C++的 回调 函数

来源:互联网 发布:淘宝类目搜索排名 编辑:程序博客网 时间:2024/05/19 16:32

回调函数的定义:

使用者自己定义一个函数,使用者自己实现这个函数的程序内容,然后把这个函数作为参数传入别人(或系统)的函数中,由别人(或系统)的函数在运行时来调用的函数。函数是你实现的,但由别人(或系统)的函数在运行时通过参数传递的方式调用,这就是所谓的回调函数。简单来说,就是由别人的函数运行期间来回调你实现的函数       

函数指针的调用方式


#include <iostream>using namespace std;typedef void(*FP)(char* s);       //类型定义为已有的数据类型取别名,常用来定义数组 指针 结构体。编译前,和define不同的是,后者预处理简单替换,前者采用定义变量一样的方法来声明类型void Invoke(char* s);int main(int argc, char* argv[]){FP fp;      //通常是用宏FP来声明一个函数指针fpfp = Invoke;fp("Hello World!\n");return 0;}void Invoke(char* s){printf(s);}

带参的回掉函数

//定义带参回调函数void PrintfText(char* s) {    printf(s);}//定义实现带参回调函数的"调用函数"void CallPrintfText(void (*callfuct)(char*),char* s)   // 形参为指向函数的指针{    callfuct(s);}//在main函数中实现带参的函数回调int main(int argc,char* argv[]){    CallPrintfText(PrintfText,"Hello World!\n"); // 把函数名作为参数,相当于将函数地址代入,参数在其后,参数相同    return 0;}

函数指针数组


#include <iostream>#include <string>using namespace std;typedef void (*FP)(char* s);void f1(char* s){cout<<s;}void f2(char* s){cout<<s;}void f3(char* s){cout<<s;}int main(int argc,char* argv[]){    void* a[]={f1,f2,f3};   //定义了指针数组,这里a是一个普通指针,数据类型是 指针,整体时数组!!!    a[0]("Hello World!\n"); //编译错误,指针数组不能用下标的方式来调用函数    FP f[]={f1,f2,f3};      //声明FP,定义一个函数指针的数组,这里的f是一个函数指针    f[0]("Hello World!\n"); //正确,函数指针的数组进行下标操作可以进行函数的间接调用        return 0;}

C Calllback example

#include <stdlib.h>#include <stdio.h>int Test1(){int i;for (i = 0; i<30; i++){printf("The %d th charactor is: %c\n", i, (char)('a' + i % 26));}return 0;}//////////////////////////////////////////////////////////////////////////////int Test2(int num){int i;for (i = 0; i<num; i++){printf("The %d th charactor is: %c\n", i, (char)('a' + i % 26));}return 0;}//////////////////////////////////////////////////////////////////////////////void Caller1(int (*ptr)())//指向函数的指针作函数参数{(*ptr)();}//////////////////////////////////////////////////////////////////////////////void Caller2(int(*ptr)(int num),int n)//指向函数的指针作函数参数,这里第一个参数是为指向函数的指针服务的,{(*ptr)(n);}//////////////////////////////////////////////////////////////////////////////int main(){printf("************************\n");Caller1(Test1);     //相当于调用Test1();printf("&&&&&&&&&&&&&&**********\n");Caller2(Test2,30); //相当于调用Test2(30);return 0;}


C++ 回调的使用方法:


#include <iostream>#include <typeinfo>class B1{private:int b1;public:void mf1();};void B1::mf1(){std::cout << "b1 = " << b1 << std::endl;}//--------------------------------class B2{private:int b2;public:void mf2();};void B2::mf2(){std::cout << "b2 = " << b2 << std::endl;}//--------------------------------class D : public B1, public B2{public:D() { d = 0; }private:int d;};void call_memfun(D obj, void(D::*pmf) ()){(obj.*pmf) ();}int main(){D obj;call_memfun(obj, &D::mf1);call_memfun(obj, &D::mf2);return 0;}

仿函数,对()进行操作符重载

#include <iostream>// 含有返回常值的函数对象的类class ConstantIntFunctor{    private:        int value;    // “函数调用”所返回的值    public:        // 构造函数:初始化返回值        ConstantIntFunctor (int c) : value(c) {}        // “函数调用”        int operator() () const {            return value;        }};// 使用上面“函数对象”的客户端函数void client (ConstantIntFunctor const& cif){    std::cout << "calling back functor yields " << cif() << '\n' ;}int main(){    ConstantIntFunctor seven(7);    ConstantIntFunctor fortytwo(42);    client(seven);    client(fortytwo);}


原创粉丝点击