函数指针就是一段函数的入口。就是一块汇编代码的起始地址,jmp xxx.
include “stdafx.h”
include “iostream”
using namespace std;
typedef int(*FP_CALC)(int, int);//定义一个函数指针类型
int add(int a, int b)
{
return a + b;
}int sub(int a, int b)
{
return a - b;
}int mul(int a, int b)
{
return a * b;
}int div_2(int a, int b)
{
return b ? a / b : -1;
}int calc(FP_CALC pf, int a, int b)
{
return pf(a, b);
}int main(int argc, _TCHAR* argv[])
{
cout << calc(add, 2, 5) << endl;
cout << calc(sub, 2, 5) << endl;
cout << calc(mul, 2, 5) << endl;
cout << calc(div_2, 2, 5) << endl;
return 0;
}