您的位置:首页 > 编程语言 > C语言/C++

C++指向函数的指针

2018-03-25 22:14 134 查看
指针,学习C和C++的人都十分了解,但是很多人都十分惧怕指针,没关系,今天我们学习指向函数的指针。

举例说明

#include<iostream>
#include<string>
using namespace std;
bool lengthCompare(const string &s1,const string &s2)
{
return s1.size() == s2.size();
}
int main()
{
//pf是一个指针,指向函数的指针。
//pf是一个局部变量。
bool (*pf)(const string &,const string &);
pf = &lengthCompare;
//	pf = lengthCompare;//这种也可以 ,因为函数的名称就在指向地址的指针
cout << lengthCompare("Hello","ASIA") << endl;;
cout << (*pf)("Hello","ASIA") << endl;
cout << pf("Hello","ASIA") << endl;
return 0;
}
bool (*pf)(const string &,const string &),好比定义一个变量,只是它是指向函数的指针。
结果如下:



我们这样定义变量的话,如果需要定义多个,就会非常麻烦,所以,我们采取类型定义的方法,来解决这个问题。//使用类型定义,定义一个函数指针
typedef bool (*comPar)(const string &,const string &);完整代码如下:#include<iostream>
#include<string>
using namespace std;
//使用类型定义,定义一个函数指针
typedef bool (*comPar)(const string &,const string &);
bool lengthCompare(const string &s1,const string &s2)
{
return s1.size() == s2.size();
}
int main()
{
//pf是一个指针,指向函数的指针。
//pf是一个局部变量。
bool (*pf)(const string &,const string &);
comPar pf2;
pf2 = lengthCompare;
pf = &lengthCompare;
// pf = lengthCompare;//这种也可以 ,因为函数的名称就在指向地址的指针
cout << lengthCompare("Hello","ASIA") << endl;;
cout << (*pf)("Hello","ASIA") << endl;
cout << pf("Hello","ASIA") << endl;
cout << pf2("Hello","ASIA") << endl;
return 0;
}


如果我们定义一个函数,除了返回类型不同,其他都一样,我们看看会出现什么情况。
定义一个函数:string::size_type sumLength(const string &s1,const string &s2)
{
return s1.size() + s2.size();
}完整如下:#include<iostream>
#include<string>
using namespace std;
//使用类型定义,定义一个函数指针
typedef bool (*comPar)(const string &,const string &);
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();
}
int main()
{
comPar pf3;
pf3 = sumLength;
cout << pf3("Hello","ASIA") << endl;
return 0;
}


出错,因为类型定义与其不符。

函数指针做形参

#include<iostream>
#include<string>
using namespace std;
//使用类型定义,定义一个函数指针
typedef bool (*comPar)(const string &,const string &);
bool lengthCompare(const string &s1,const string &s2)
{
return s1.size() == s2.size();
}
void useBigger(const string &s1,const string &s2,bool (*pf)(const string &,const string &))
{
cout << pf(s1,s2) << endl;;
}
int main()
{
comPar pf2;
pf2 = lengthCompare;
useBigger("Hello","ASIA",pf2);
useBigger("Hello","ASIA",lengthCompare);
return 0;
}



这个非常有意思,指向函数的指针可以做形参。#include<iostream>
#include<string>
using namespace std;
int demo(int *x,int y)
{
cout << "demo" <<endl;
return 16;
}
//ff是一个函数,有一个形参x返回结果是一个函数指针int(*)(int*,int)
int (*ff(int x))(int *,int )
{
cout << x <<endl;
return demo;
}
int main()
{
int a = 3;
cout << ff(4)(&a,a) << endl;
return 0;
}


当然这看起来比较复杂,我们采用类型定义。如下:#include<iostream>
#include<string>
using namespace std;
typedef int (*PF)(int *,int );
int demo(int *x,int y)
{
cout << "demo" <<endl;
return 16;
}
//ff是一个函数,有一个形参x返回结果是一个函数指针int(*)(int*,int)
PF ff(int x)
//int (*ff(int x))(int *,int )
{
cout << x <<endl;
return demo;
}
int main()
{
int a = 3;
cout << ff(4)(&a,a) << endl;
return 0;
}

指向重载函数的指针

#include<iostream>
#include<string>
using namespace std;
void demo(double x)
{
cout << "demo(double x)" <<endl;
}
void demo(unsigned int x)
{
cout << "demo(unsigned int x)" << endl;
}
int main()
{
//void (*pf)(int) = &demo;
void (*pf)(double) = &demo;
return 0;
}
指向重载函数的指针必须精确匹配,否则出错。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: