c/c++:回调函数

File : cbfunc.cpp
Type : c/c++
Brief : callback function


/*
 * Callback Function
 * 回调函数
 *
 */


#include <iostream>
using namespace std;

// 回调函数定义
typedef void(*cbFunc)(const char* s);

// understand like this:
//typedef   void(*)(char *s)    cbFunc;

void cbPrintStr1(cbFunc func, const char* s)
{
    cout << "this is cbPrintStr 1" << endl;
    func(s);
}

void cbPrintStr2(void (*func)(const char* s), const char *s)
{
    cout << "this is cbPrintStr 2" << endl;
    func(s);
}

void outStr(const char* s)
{
    cout << s << endl;
}

int main(int argc, char* argv[])
{
    cbPrintStr1(outStr, "1st operation by outStr");
    cout << endl;

    cbPrintStr2(outStr, "2nd operation by outStr");
    cout << endl;

    cbFunc cbFp;
    cbFp = outStr;
    cbPrintStr1(cbFp, "3th operation by cbFp");
    cout << endl;
    
    cbPrintStr2(cbFp, "4th operation by cbFp");

    return 0;
}

转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 [ yehuohan@gmail.com ]

文章标题:c/c++:回调函数

本文作者:Y

发布时间:2017-08-09, 13:20:28

最后更新:2019-05-21, 20:04:47

原始链接:http://yehuohan.github.io/2017/08/09/Gist/c&c++/callback-function/

版权声明: "署名-非商用-相同方式共享 4.0" 转载请保留原文链接及作者。