C++之指向函数的指针

 1 #include <iostream>
 2 #include <string>
 3 //#include "Sales_item.h"
 4 #include "Sales_item.cpp"
 5 
 6 using  namespace std;
 7 //使用typedef简化定义
 8 typedef bool (*pf)(const string &s1,const string &s2);
 9 typedef int (*PF)(int *, int);
10 
11 
12 bool length(const string &s1,const string &s2)
13 {
14     return s1.size()==s2.size();
15 }
16 
17 void useBigger(const string &s1,const string &s2,bool (*pf)(const string &s1,const string &s2))
18 {
19     cout<<length(s1,s2)<<endl;
20 }
21 //ff是一个函数名 有一个形参 x 返回的结果为一个函数指针int(*)(int *,int)
22 //int (*ff(int x))(int *, int)
23 //{
24 //
25 //}
26 //等同于上边的定义
27 //PF(*ff(int x))
28 //{
29 //
30 //}
31 
32 int main()
33 {
34     //指向bool类型的函数指针
35     //bool (*pf)(const string &,const string &);
36     pf pf1; //必须指向同类型的函数
37     //pf=&length;
38     pf pf2;
39     cout<<length("hello","pointer")<<endl;
40     cout<<(*pf1)("hello","pointer")<<endl;
41     useBigger("hello","pointer",pf2);
42     return 0;
43     //预处理进行调试
44     #ifdef NDEBUG
45     cout<<""<<endl;
46     #endif // NDEBUG
47 }

 

posted @ 2020-03-29 09:14  萌萌~  阅读(588)  评论(0编辑  收藏  举报