ca71a_c++_指向函数的指针_通过指针调用函数txwtech

/*ca71a_c++_指向函数的指针_通过指针调用函数
用typedef简化函数指针的定义
简化前:
bool(*pf)(const string&, const string &);
bool(*pf2)(const string&, const string &);
bool(*pf3)(const string&, const string &);

简化后:
typedef bool(*cmpFcn)(const string &, const string &);
//具体应用参考main函数内部代码
comFcn pf;
comFcn pf2;
comFcn pf3;
指向函数的指针的初始化和赋值
通过指针调用函数
函数指针形参
返回指向函数的指针

//ff是一个函数,有一个形参x,返回的结果是一个函数指针int(*)(int *,int),
int(*ff(int x))(int *, int)

{
cout << x << endl;
return demo;
}
调用时,首先执行ff(int x),然后返回*int的指针,然后指向demo,然后把demo(int *, int)传递给demo函数的定义
int demo(int *p, int a)
{
return 8;
}
使用typedef简化,一遍更好的理解:
typedef int (*PF)(int *, int);

PF ff(int x)
{
cout << x << endl;
return demo;//指针指向demo,调用demo函数
}

//pf是一个指针,指向函数的指针:函数类型
bool(*pf)(const string&, const string &);
//
//bool *pf(const string&, const string &);
*pf如果没有括号,pf就是一个函数了,它的返回值是指向bool的一个指针

返回类型不一样,或者形参不一样,也不可以指向


指向重载函数的指针:

//void(*pff)(int) = &ff;//需要精确匹配
void (*pfg)(vector<double>) = &ff;
//double(*pfg1)(vector<double>) = &ff;//错误,类型不匹配
void(*pfg2)(unsigned int) = &ff;

*/

  1 /*ca71a_c++_指向函数的指针_通过指针调用函数
  2 用typedef简化函数指针的定义
  3 简化前:
  4     bool(*pf)(const string&, const string &);
  5     bool(*pf2)(const string&, const string &);
  6     bool(*pf3)(const string&, const string &);
  7 
  8 简化后:
  9     typedef bool(*cmpFcn)(const string &, const string &);
 10     //具体应用参考main函数内部代码
 11     comFcn pf;
 12     comFcn pf2;
 13     comFcn pf3;
 14 指向函数的指针的初始化和赋值
 15 通过指针调用函数
 16 函数指针形参
 17 返回指向函数的指针
 18 
 19 //ff是一个函数,有一个形参x,返回的结果是一个函数指针int(*)(int *,int),
 20 int(*ff(int x))(int *, int)
 21 
 22 {
 23     cout << x << endl;
 24     return demo;
 25 }
 26 调用时,首先执行ff(int x),然后返回*int的指针,然后指向demo,然后把demo(int *, int)传递给demo函数的定义
 27 int demo(int *p, int a)
 28 {
 29     return 8;
 30 }
 31 使用typedef简化,一遍更好的理解:
 32 typedef int (*PF)(int *, int);
 33 
 34 PF ff(int x)
 35 {
 36 cout << x << endl;
 37     return demo;//指针指向demo,调用demo函数
 38 }
 39 
 40 //pf是一个指针,指向函数的指针:函数类型
 41 bool(*pf)(const string&, const string &);
 42 //
 43 //bool *pf(const string&, const string &);
 44 *pf如果没有括号,pf就是一个函数了,它的返回值是指向bool的一个指针
 45 
 46 返回类型不一样,或者形参不一样,也不可以指向
 47 
 48 
 49 指向重载函数的指针:
 50 
 51 //void(*pff)(int) = &ff;//需要精确匹配
 52     void (*pfg)(vector<double>) = &ff;
 53 //double(*pfg1)(vector<double>) = &ff;//错误,类型不匹配
 54     void(*pfg2)(unsigned int) = &ff;
 55 
 56 */
 57 
 58 #include <iostream>
 59 #include <string>
 60 #include <vector>
 61 using namespace std;
 62 
 63 //函数定义简化操作
 64 typedef bool(*cmpFcn)(const string &, const string &);
 65 typedef int (*PF)(int *,int);
 66 
 67 
 68 bool lengthCompare(const string &s1, const string &s2)
 69 {
 70     return s1.size() == s2.size();//比较长度
 71 }
 72 string::size_type sumlength(const string &s1, const string &s2)
 73 {
 74     return s1.size() + s2.size();//长度相加
 75 }
 76 bool cstringCompare(char *s1, char *s2)
 77 {
 78     return strlen(s1) == strlen(s2);
 79 }
 80 void useBigger(const string &s1, const string &s2, bool(*pf)(const string &, const string &))//函数指针形参
 81 {
 82     cout << pf(s1, s2) << endl;
 83 }
 84 int demo(int *p, int a)
 85 {
 86     cout << "执行demo()" << endl;
 87     return 8;
 88 }
 89 //ff是一个函数,有一个形参x,返回的结果是一个函数指针int(*)(int *,int),
 90 //int(*ff(int x))(int *, int)//这句也可以
 91 PF ff(int x)//经过typedef简化后:
 92 {
 93     cout << "执行ff(int x)" << endl;
 94     cout << x << endl;
 95     return demo;
 96 }
 97 //指向重载函数的指针:
 98 
 99 void  ff(vector<double> vec)
100 {
101     cout << "ff(vector<double> vec)" << endl;
102 }
103 void ff(unsigned int x)
104 {
105     cout << "ff(unsigned int x)" << endl;
106 }
107 int main()
108 {
109     int a = 5;
110     int *pa;
111     //pf是一个指针,指向函数的指针:函数类型
112     bool(*pf)(const string&, const string &);//pf是一个局部变量
113     bool(*pf2)(const string&, const string &);
114     bool(*pf3)(const string&, const string &);
115     pf = &lengthCompare;//也可以写成:pf=lengthCompare;
116     
117     pa = &a;//普通指针
118     cout << lengthCompare("hello", "pointer") << endl;
119     cout << lengthCompare("hello", "point") << endl;
120     cout << (*pf)("hello", "point") << endl;//函数指针
121     //也可以写成这样:*号省掉
122     cout << pf("hello", "point") << endl;//函数指针
123 
124     cout << *pa << endl;
125 
126     //bool(*pf)(const string&, const string &);被简化为如下:
127     cmpFcn pfa;
128     cmpFcn pf2a = 0;//指向函数的指针的初始化
129     cmpFcn pf3a = 0;
130     pf3 = pf2a;//指向函数的指针的赋值
131     //然后调用方法一样:
132     pfa = &lengthCompare; //指向函数的指针的赋值
133     pf2 = lengthCompare;
134     //pf3 = sumlength; //指针指向的返回值需要一致。不能bool指向 size_type.
135     cout << (*pfa)("hello", "point") << endl;//函数指针
136     cout << pf2("bb","cc") << endl;//也可以写成这样:*号省掉
137 
138     cmpFcn pf4 = lengthCompare;
139     useBigger("hi","function",pf4);
140     cout << ff(2)(&a,a) << endl;
141 
142     //指向重载函数的指针:
143 
144     //void(*pff)(int) = &ff;//需要精确匹配
145     void (*pfg)(vector<double>) = &ff;
146    //double(*pfg1)(vector<double>) = &ff;//错误,类型不匹配
147     void(*pfg2)(unsigned int) = &ff;
148     return 0;
149 }
欢迎讨论,相互学习。 txwtech@163.com
原文地址:https://www.cnblogs.com/txwtech/p/12290480.html