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;

*/

 /*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; */ #include <iostream>
#include <string>
#include <vector>
using namespace std; //函数定义简化操作
typedef bool(*cmpFcn)(const string &, const string &);
typedef int (*PF)(int *,int); bool lengthCompare(const string &s1, const string &s2)
{
return s1.size() == s2.size();//比较长度
}
string::size_type sumlength(const string &s1, const string &s2)
{
return s1.size() + s2.size();//长度相加
}
bool cstringCompare(char *s1, char *s2)
{
return strlen(s1) == strlen(s2);
}
void useBigger(const string &s1, const string &s2, bool(*pf)(const string &, const string &))//函数指针形参
{
cout << pf(s1, s2) << endl;
}
int demo(int *p, int a)
{
cout << "执行demo()" << endl;
return ;
}
//ff是一个函数,有一个形参x,返回的结果是一个函数指针int(*)(int *,int),
//int(*ff(int x))(int *, int)//这句也可以
PF ff(int x)//经过typedef简化后:
{
cout << "执行ff(int x)" << endl;
cout << x << endl;
return demo;
}
//指向重载函数的指针: void ff(vector<double> vec)
{
cout << "ff(vector<double> vec)" << endl;
}
void ff(unsigned int x)
{
cout << "ff(unsigned int x)" << endl;
}
int main()
{
int a = ;
int *pa;
//pf是一个指针,指向函数的指针:函数类型
bool(*pf)(const string&, const string &);//pf是一个局部变量
bool(*pf2)(const string&, const string &);
bool(*pf3)(const string&, const string &);
pf = &lengthCompare;//也可以写成:pf=lengthCompare; pa = &a;//普通指针
cout << lengthCompare("hello", "pointer") << endl;
cout << lengthCompare("hello", "point") << endl;
cout << (*pf)("hello", "point") << endl;//函数指针
//也可以写成这样:*号省掉
cout << pf("hello", "point") << endl;//函数指针 cout << *pa << endl; //bool(*pf)(const string&, const string &);被简化为如下:
cmpFcn pfa;
cmpFcn pf2a = ;//指向函数的指针的初始化
cmpFcn pf3a = ;
pf3 = pf2a;//指向函数的指针的赋值
//然后调用方法一样:
pfa = &lengthCompare; //指向函数的指针的赋值
pf2 = lengthCompare;
//pf3 = sumlength; //指针指向的返回值需要一致。不能bool指向 size_type.
cout << (*pfa)("hello", "point") << endl;//函数指针
cout << pf2("bb","cc") << endl;//也可以写成这样:*号省掉 cmpFcn pf4 = lengthCompare;
useBigger("hi","function",pf4);
cout << ff()(&a,a) << endl; //指向重载函数的指针: //void(*pff)(int) = &ff;//需要精确匹配
void (*pfg)(vector<double>) = &ff;
//double(*pfg1)(vector<double>) = &ff;//错误,类型不匹配
void(*pfg2)(unsigned int) = &ff;
return ;
}
上一篇:Python脚本控制的WebDriver 常用操作 <二十七> 文件下载


下一篇:java io流中怎么在一个文本中追加字符串