话说最近参加了2个校园招聘的笔试,结果都悲剧了 - -!因为都是考的C/C++!而这一块貌似我2年都没有看过了,好多东西似是而非的。考试的结果自然是可想而知的了
所以痛定思痛的决定复习下C++,希望能亡羊补牢吧(神说:校园招聘都要结束了,你才想起复习下 - -!.5555可惜我一直以为校园招聘是大4下学期才开始的,等我知道的时候都要结束了 - -)。
一、函数指针形参
函数的形参可以是指向函数的指针,这种形参可以有以下两种方式编写。
1 | void fun(const int &,const int &,bool (const int &,const int &)); |
1 | void fun(const int &,const int &,bool (*)(const int &,const int &)); |
二、返回指向函数的指针
函数可以返回指向函数的指针,书写方式如下
1 2 3 | bool (*getFun(int type=1)) (const int &x,const int &y); //声明一个叫getFun的函数,它带有一个int型的形参。 //该函数将返回 bool (*) (const int &x,const int &y) 的一个指向函数的指针。 |
三、一些相关的例子
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | bool bigger(const int &x,const int &y) { return x>y; } bool smaller(const int &x,const int &y) { return x<y; } bool (*getFun(int type)) (const int &x,const int &y) { return type ? bigger : smaller; } bool doit(const int &x,const int &y,bool (*ff) (const int &x,const int &y) ) { return ff(x,y); } |
一个为了例子而生的调用方式。不要去计较getFun()的冗余
1 2 3 4 5 6 7 8 9 10 | int main() { if(doit(5,4,getFun())) { cout<<"YES"<<endl; } system("pause") ; return 0 ; } |