2009年4月25日 星期六

callback function 範例

#include <cstdlib>
#include <iostream>
using namespace std;

typedef int(*Binary_FT)(int,int);

int multiplyCB(int x, int y)
{
return x * y;
}

int additionCB(int x, int y)
{
return x + y;
}

void printFrameWork(int a, int b, char *str, Binary_FT callback)
{
int c;
c = callback(a, b);
printf("callback = %s\n", str);
printf("result = %d\n", c);
}

int main(int argc, char *argv[])
{
int integer1, integer2;
integer1 = 10;
integer2 = 20;

printf("Two inputs are %d and %d\n", integer1, integer2);
printFrameWork(integer1, integer2, "multiplyCB", multiplyCB);
printFrameWork(integer1, integer2, "additionCB", additionCB);

system("PAUSE");
return EXIT_SUCCESS;
}

執行結果:
Two inputs are 10 and 20
callback = multiplyCB
result = 200
callback = additionCB
result = 30

沒有留言: