C++指向函数的指针

指针,学习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;
}

指向重载函数的指针必须精确匹配,否则出错。






  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值