C++之函数指针
(1)函数指针的声明
所谓函数指针是指指向函数而非对象的指针,这个指针所指向的函数的类型由返回类型和参数表确定,与函数名无关。例如:
bool (*pf)(const string& a, const string& b);
pf是一个指向函数的指针,该函数的形参是两个const string&类型的字符串,返回值是一个bool类型的值。需要注意的是上面的声明中不等价于下面的写法:
bool *pf(const string& a, const string& b);
因为上面是一个普通的函数声明,它的形参也是两个const string&类型的字符串,返回值是一个指向bool对象的指针,函数名为pf。
(2)函数指针的初始化和赋值
这里首先使用typedef简化函数指针的定义:
typdef bool (*pf)(const string&, const string&);
对函数指针的初始化有三种方式:
a. 0值常量表达式
pf myPf1 = 0;
b. 直接饮用函数名或者在函数名上应用取地址操作符
1 bool compareLength(const string& sa, const string& sb); 2 pf myPf1 = compareLength; 3 pf myPf2 = &compareLength;
c. 同类型的函数指针
myPf2 = myPf1;
(3)通过函数指针调用函数
指向函数的指针可以通过解引用符调用函数,也可以直接调用它所指向的函数:
pf myPf1 = compareLength; bool bVal = compareLength("hello", "welcome"); bool bVal = myPf1("hello", "welcome"); bool bVal = (*myPf1)("hello","welcome");
(4)函数指针作为形参
函数指针可以作为形参,具体的使用方式有两种:第一种是直接以函数类型的形式作为形参,这种形式下编译器隐式地把它当做指向该函数类型的函数指针;第二种是显示的传递一个指向某个函数类型的函数指针。例如:
void GetLonger(const string&, const string&, bool(const string&, const string&)); void GetLonger(const string&, const string&, bool (*)(const string&, const string&));
(5)函数指针作为返回类型
函数指针也可以作为一个函数的返回类型,但是不同与函数指针作为形参的情形:函数类型可以作为形参,但不可以做返回类型,只能将指向函数的指针作为返回类型。例如:
int (*ff(int))(int*, int);
上面的代码表示函数ff需要一个int类型的参数,返回一个指向函数类型为int(int*, int)函数指针。也可以借助typedef定义如下:
typedef int (*pf)(int*, int); pf ff(int);
(6)指向重载函数的函数指针
C++语言允许使用函数指针指向重载的函数:
1 extern void ff(vector<double>); 2 extern void ff(unsigned int); 3 4 void (*pf)(unsigned int) = ff;
函数指针指向重载的函数时,指针的类型必须与重载函数的一个版本精确匹配,否则对该指针的初始化或赋值都会导致编译错误!
(7)示例代码
1 #include "stdafx.h" 2 3 #include<iostream> 4 using namespace std; 5 6 #include<string> 7 #include<vector> 8 9 typedef int (*PF)(const string&, const string&); 10 int compareLength(const string& s1, const string& s2) 11 { 12 if(s1.length() > s2.length()) 13 { 14 return 1; 15 } 16 return (s1.length() == s2.length() ? 0 : -1); 17 } 18 19 //函数指针作为形参 20 const string& GetLonger(const string& s1, const string& s2, int (*pf)(const string&, const string&)) 21 { 22 int res = pf(s1,s2); 23 if (res == 1) 24 { 25 return s1; 26 } 27 return s2; 28 } 29 30 const string str_cat(const string& s1, const string& s2) 31 { 32 string s = s1 + s2; 33 return s; 34 } 35 36 //函数指针作为返回类型 37 const string (*GetPf(const string& s1, const string& s2))(const string&, const string&) 38 { 39 const string (*pf)(const string& s1, const string& s2) = str_cat; 40 return pf; 41 } 42 43 //函数指针指向重载的函数 44 void ff(vector<int>) 45 { 46 cout<<"overload function 1"<<endl; 47 } 48 49 void ff(int a) 50 { 51 cout<<"overload function 2"<<endl; 52 } 53 54 int _tmain(int argc, _TCHAR* argv[]) 55 { 56 PF comPf = compareLength; 57 string s1("hello"); 58 string s2("hell"); 59 60 //函数指针调用函数 61 int compareRes1 = comPf(s1,s2); 62 int compareRes2 = (*comPf)(s1,s2); 63 cout<<compareRes1<<" "<<compareRes2<<endl; 64 65 //函数指针作为形参 66 const string getS1 = GetLonger(s1,s2,comPf); 67 cout<<getS1<<endl; 68 69 //返回函数指针 70 const string (*getPf)(const string& s1, const string& s2) = GetPf(s1, s2); 71 string catRes = getPf(s1,s2);//调用返回的函数指针 72 cout<<catRes<<endl; 73 74 //指向重载函数的函数指针 75 void (*ofunc)(int) = ff; 76 ofunc(3); 77 78 system("pause"); 79 80 return 0; 81 }
以上整理自C++ Primer中文版第四版 7.9 节。