回调函数理解实例

// callback.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdio.h>
 
void printWelcome(int len)
{
       printf("欢迎欢迎 -- %d\n", len);
}
 
void printGoodbye(int len)
{
       printf("送客送客 -- %d\n", len);
}
 
void callback(int times, void (* print)(int))
{
       int i;
       for (i = 0; i < times; ++i)
       {
              print(i);
       }
       printf("\n我不知道你是迎客还是送客!\n\n");
}
void main(void)
{
       callback(10, printWelcome);
       callback(10, printGoodbye);
       printWelcome(5);
       getchar();
}

乱翻的时候看到这段代码,对于理解回调函数十分有效。

不扯系统函数,不扯dll,例子中callback中调用了printWelcome函数。这就是回调函数。不要想的太深,等遇到实际情况的时候你想想例子,原来就是这么回事。

posted @ 2017-05-24 16:23  lcamry  阅读(330)  评论(1编辑  收藏  举报