转载自:http://www.cnblogs.com/TenosDoIt/p/3164081.html
1. 概念
函数指针,顾名思义就是指向函数的指针。函数具有可赋值给指针的物理内存地址,一个函数的函数名就是一个指针,它指向函数的代码。一个函数的地址是该函数的进入点,也是调用函数的地址。函数的调用可以通过函数名,也可以通过指向函数的指针来调用。函数指针还允许将函数作为变元传递给其他函数。
定义形式:类型 (*指针变量名)(参数列表);
例如:int (*p)(int i,int j);
其中,p是一个指针,它指向一个函数,该函数有2个整形参数,返回类型为int。p首先和*结合,表明p是一个指针。然后再与()结合,表明它指向的是一个函数。指向函数的指针也称为函数指针。
2. 使用方式
2.1 普通使用方法
#include <iostream>
using namespace std;
class test {
public:
test()
{
cout << "constructor" << endl;
}
int fun1(int a, char c)
{
cout << "this is fun1 call: " << a << " " << c << endl;
return a;
}
void fun2(double d) const
{
cout << "this is fun2 call: " << d << endl;
}
static double fun3(char buf[])
{
cout << "this is fun3 call: " << buf << endl;
return 3.14;
}
};
int main()
{
// 类的静态成员函数指针
double (*pstatic)(char buf[]) = NULL;//不需要加类名
pstatic = test::fun3; //可以不加取地址符号
pstatic("myclaa");
pstatic = &test::fun3;//加取地址符号
(*pstatic)("xyz");
//普通成员函数
int (test::*pfun)(int, char) = NULL; //一定要加类名
pfun = &test::fun1; //一定要加取地址符号
test mytest;
(mytest.*pfun)(1, 'a'); //调用时一定要加类的对象名和*符号
//const 函数(基本普通成员函数相同)
void (test::*pconst)(double)const = NULL; //一定要加const
pconst = &test::fun2;
test mytest2;
(mytest2.*pconst)(3.33);
return 0;
}
结果如下:
this is fun3 call: myclaa
this is fun3 call: xyz
constructor
this is fun1 call: 1 a
constructor
this is fun2 call: 3.33
2.2 作为函数参数
#include <iostream>
using namespace std;
void fun(int k, char c)
{
cout << "this is fun2 call: " << k << ", " << c << endl;
}
void fun1(void (*pfun)(int, char), int a, char c)
{
pfun(a, c);
}
int main()
{
fun1(fun, 1, 'a');
return 0;
}
结果如下:
this is fun2 call: 1, a
2.3 作为函数返回值
//
//函数指针作为返回值
//
#include <iostream>
using namespace std;
class test1
{
public:
int fun(int a, char c)
{
cout << "this is fun call:" << a << " " << c << endl;
return a;
}
};
class test2
{
public:
//成员函数:fun1(double d)
//返回值:int(test::*)(int, char),是test1的成员函数指针
int (test1::*fun1(double d))(int, char)
{
cout << d << endl;
return &test1::fun;
}
};
int main()
{
test1 mytest1;
test2 mytest2;
int (test1::*p)(int, char) = mytest2.fun1(3.33);
(mytest1.*p)(1, 'a');
return 0;
}
2.4 函数指针数组
//
//函数指针数组
//
#include <iostream>
using namespace std;
float add(float a, float b){
return a + b;
}
float minu(float a, float b){
return a - b;
}
int main()
{
//定义一个函数指针数组,大小为2
//里面存放float (*)(float, float)类型的指针
float (*pfunArry[2])(float, float) = {&add, &minu};
double k = pfunArry[0](3.33,2.22);// 调用
cout << "add: " << k << endl;
k = pfunArry[1](3.33,2.22);
cout << "minu: " << k << endl;
return 0;
}
2.5 typedef简化
//
//typedef定义函数指针
//
#include <iostream>
using namespace std;
float add(float a, float b)
{
cout << "add: " << a + b << endl;
return a + b;
}
float minu(float a, float b)
{
cout << "minu: " << a - b << endl;
return a - b;
}
//用pfunType 来表示float(*)(float, float)
typedef float(*pfunType)(float, float);
int main()
{
pfunType p = &add;//定义函数指针变量
p(3.33, 2.22);
pfunType parry[2] = {&add, &minu};//定义函数指针数组
parry[1](3.33, 2.22);
//函数指针作为参数可以定义为:void fun(pfunType p)
//函数指针作为返回值可以定义为:pfunType fun();
return 0;
}