1 简单的函数指针的应用,形如:返回类型 ( *函数名 )( 参数列表 )
#include "stdafx.h"
#include <iostream>
int ( *pFunc )( int, int );
int Func( int x, int y )
{
return x + y;
}
void main()
{
pFunc = Func;
int nSum = ( *pFunc )( 100, 100 );
std::cout << nSum << std::endl;
system( "pause" );
}
函数的函数名实际上就是一个指针,指向该函数的代码在内存中的首地址。
2 使用typedef定义函数指针,形如:typedef 返回类型 ( *新类型 )( 参数列表 )
#include "stdafx.h"
#include <iostream>
typedef int ( *PTRFUNC )( int, int );
int Func( int x, int y )
{
return x + y;
}
void main()
{
PTRFUNC pFunc;
pFunc = Func;
int nSum = ( *pFunc )( 100, 100 );
std::cout << nSum << std::endl;
system( "pause" );
}
typedef的功能是定义新类型,使用新类型定义一个新的变量,为这个变量赋值。