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 }

原文链接: https://www.cnblogs.com/yh2924/p/12590859.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/194986

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月12日 下午6:51
下一篇 2023年2月12日 下午6:52

相关推荐