C++函数指针

这次来理一下函数指针的一些用法,首先我们看一段简单的代码,我们后面所讲解的指向函数的指针都基于这段代码之上来改变的。


#include<iostream>
using namespace std;
int min(const int* p,const int length);
void main()
{
	int length=0,*p=nullptr;
	cout<<"Please Enter array length:"<<endl;
	cin>>length;
	cout<<"Please enter the number "<<length<<":"<<endl;
	p=new int[length];
	for (int i = 0; i < length; i++)
	{
		cin>>*(p+i);
	}
	cout<<"Call min function get result is "<<min(p,length)<<" . "<<endl;
	delete [] p;
	p=nullptr;
}
/**
该方法用于计算一组数据中的最小值
*/
int min(const int* p,const int length)
{
	int min=*p;
	for (int i = 0; i < length; i++)
	{
		if (min>*(p+i))
		{
			min=*(p+i);
		}
	}
	return min;
}

这当中的min方法,就是用于计算数据中的最小值,我们看看main方法中,

首先,要求输入数组的长度,然后我们使用指针数组,动态创建一个数组

接下来,根据输入的长度依次录入数据到数组中,

然后调用min方法输出结果,最后释放指针数组在将指针赋空


很简单的一段代码,自己可以测试一下。。。


接下来我们就来介绍 函数 指针了。


1.函数指针的基本概念

函数指针所谓的就是指向函数的指针,每个函数都有一个入口地址,函数名就是这个入口地址,调用函数时需要从这个地址中进去。故而指针是专门用来指向内存地址的,所以有函数指针这一说法,,,那么函数指针通常是干嘛用的呢??

C#中有委托事件这一知识点,,Java就不知道了,  C++中的函数指针就是干这事的,用于函数回调,它可以将函数通过参数传递给另外一个函数进行调用

2.函数指针的声明和初始化

函数指针的声明:
函数的返回类型    (*函数指针名) (参数列表)

如:
int (*pMin)(const int* p,const int length);

这里的第一个括号切记别省略了,不然就成了这样

int *pMin(const int* p,const int length);

这就是一个返回int指针的函数pMin的声明了  

函数指针的初始化:

函数指针名=函数名;
如:
pMin=min;
也可以把声明和初始化放到一行:
int (*pMin)(const int* p,const int length)=min;


3.函数指针的调用

#include<iostream>
using namespace std;
int min(const int* p,const int length);
void main()
{
	int (*pFun)(const int* p,const int length)=min;
	int length=0,*p=nullptr;
	cout<<"Please Enter array length:"<<endl;
	cin>>length;
	cout<<"Please enter the number "<<length<<":"<<endl;
	p=new int[length];
	for (int i = 0; i < length; i++)
	{
		cin>>*(p+i);
	}
	cout<<"Call min function get result is "<<pFun(p,length)<<" . "<<endl;
	delete [] p;
	p=nullptr;
}
/**
该方法用于计算一组数据中的最小值
*/
int min(const int* p,const int length)
{
	int min=*p;
	for (int i = 0; i < length; i++)
	{
		if (min>*(p+i))
		{
			min=*(p+i);
		}
	}
	return min;
}

这是基于最开始的代码改的,就加了一句函数指针的声明和初始化,,最后在用方法名调用的时候改成了用函数指针名调用了。

函数指针的用法就这么简单,接下来我们来看看函数指针数组的使用

4.函数指针数组

#include<iostream>
using namespace std;
int min(const int* p,const int length);
int max(const int* p,const int length);
//利用typedef宏声明一个指向相应函数的指针类型
typedef int (*pFun)(const int* p,const int length);
void main()
{
	//可以和使用其他类型一样使用函数指针类型
	pFun myfun[]={min,max};

	int length=0,*p=nullptr;
	cout<<"Please Enter array length:"<<endl;
	cin>>length;
	cout<<"Please enter the number "<<length<<":"<<endl;
	p=new int[length];
	for (int i = 0; i < length; i++)
	{
		cin>>*(p+i);
	}
	//利用函数指针数组调用相对应下标的函数
	cout<<"Call min function get result is "<<myfun[0](p,length)<<" . "<<endl;
	cout<<"Call max function get result is "<<myfun[1](p,length)<<" . "<<endl;
	delete [] p;
	p=nullptr;
}
/**
该方法用于计算一组数据中的最小值
*/
int min(const int* p,const int length)
{
	int min=*p;
	for (int i = 0; i < length; i++)
	{
		if (min>*(p+i))
		{
			min=*(p+i);
		}
	}
	return min;
}
/**
该方法用于计算一组数据中的最大值
*/
int max(const int* p,const int length)
{
	int min=*p;
	for (int i = 0; i < length; i++)
	{
		if (min<*(p+i))
		{
			min=*(p+i);
		}
	}
	return min;
}

这里新增了一个计算最大值的方法,

然后利用typedef宏定义了一个指向函数指针的类型,

然后再用这个类型创建了一个数组,并赋值

下面在和使用数组一样用下标来调用函数

最后调用也可以使用指针的形式,我们知道指针和数组之间的关系,所以这里特别灵活,脑袋里面如果没有清晰的内存分布图是绕不清这逻辑的

cout<<"Call min function get result is "<<(*myfun)(p,length)<<" . "<<endl;
	cout<<"Call max function get result is "<<(*(myfun+1))(p,length)<<" . "<<endl;

接下来我们在来看看函数指针返回值

5.函数指针返回值

前面我们使用的都有返回int类型的值,那么没有返回值得情况呢??看下面的代码,代码有点长,不过是从上面代码扩展下来的
#include<iostream>
#include<string>
#include<sstream>
using namespace std;
int min(const int* p,const int length);
int max(const int* p,const int length);
string toString(int number);
void printStr(string str);
//利用typedef宏声明一个指向相应函数的指针类型
typedef int (*pFun)(const int* p,const int length);
typedef void (*pFun2)(string str);
void main()
{
	//可以和使用其他类型一样使用函数指针类型
	pFun myfun[]={min,max};
	pFun2 myfun2=printStr;
	int length=0,*p=nullptr;
	myfun2("Please Enter array length:");
	cin>>length;

	myfun2("Please Enter array length "+toString(length)+":");


	p=new int[length];
	for (int i = 0; i < length; i++)
	{
		cin>>*(p+i);
	}


	//利用函数指针数组调用相对应下标的函数
	cout<<"Call min function get result is "<<(*myfun)(p,length)<<" . "<<endl;
	cout<<"Call max function get result is "<<(*(myfun+1))(p,length)<<" . "<<endl;
	delete [] p;
	p=nullptr;
}
/**
该方法用于计算一组数据中的最小值
*/
int min(const int* p,const int length)
{
	int min=*p;
	for (int i = 0; i < length; i++)
	{
		if (min>*(p+i))
		{
			min=*(p+i);
		}
	}
	return min;
}
/**
该方法用于计算一组数据中的最大值
*/
int max(const int* p,const int length)
{
	int min=*p;
	for (int i = 0; i < length; i++)
	{
		if (min<*(p+i))
		{
			min=*(p+i);
		}
	}
	return min;
}
/**
打印字符串的方法
*/
void printStr(string str)
{
	cout<<str<<endl;
}
/**
int转字符串
*/
string toString(int number)
{
	stringstream ss;
	ss<<number;
	string str=ss.str();
	return str;
}

6.函数指针用作参数进行回调

#include<iostream>
#include<string>
#include<sstream>
using namespace std;
//利用typedef宏声明一个指向相应函数的指针类型
typedef int (*pFun)(const int* p,const int length);
typedef void (*pFun2)(string str);
//声明方法
int min(const int* p,const int length);
int max(const int* p,const int length);
string toString(int number);
void printStr(string str);
//回调方法
int getResult(pFun p,const int* arrays,const int length);
void main()
{
	//可以和使用其他类型一样使用函数指针类型
	pFun myfun[]={min,max};
	pFun2 myfun2=printStr;
	int length=0,*p=nullptr;
	myfun2("Please Enter array length:");
	cin>>length;

	myfun2("Please Enter array length "+toString(length)+":");


	p=new int[length];
	for (int i = 0; i < length; i++)
	{
		cin>>*(p+i);
	}


	//利用函数指针数组调用相对应下标的函数
	cout<<"Call min function get result is "<<getResult((*myfun),p,length)<<" . "<<endl;
	cout<<"Call max function get result is "<<getResult((*(myfun+1)),p,length)<<" . "<<endl;
	delete [] p;
	p=nullptr;
}
/**
该方法用于计算一组数据中的最小值
*/
int min(const int* p,const int length)
{
	int min=*p;
	for (int i = 0; i < length; i++)
	{
		if (min>*(p+i))
		{
			min=*(p+i);
		}
	}
	return min;
}
/**
该方法用于计算一组数据中的最大值
*/
int max(const int* p,const int length)
{
	int min=*p;
	for (int i = 0; i < length; i++)
	{
		if (min<*(p+i))
		{
			min=*(p+i);
		}
	}
	return min;
}
/**
打印字符串的方法
*/
void printStr(string str)
{
	cout<<str<<endl;
}
/**
int转字符串
*/
string toString(int number)
{
	stringstream ss;
	ss<<number;
	string str=ss.str();
	return str;
}
/**
用于获得结果的方法,演示函数指针用作方法回调
*/
int getResult(pFun p,const int* arrays,const int length)
{
	return p(arrays,length);
}

主要看新增的getResult方法和它的调用方式

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值