c++函数指针和typedef函数指针

**要点a:**函数指针就是和指针一样,里面就是存放个地址
**要点b:**有参数的函数指针可以按类型解析数据

要点c:typdef 定义的函数指针

要点a,b

#include <iostream> 
#include "windows.h"
using namespace std;

//要点a,b

 void  (* FUNTION)(int ,int);  //函数指针
							 //就类似这样,其中FUNTION是一个指针;

void temp(int x, int y)
{

	cout << x << y << endl;
}
int main()
{	
	FUNTION = &temp;	//FUNTION是指针总要给它个地址吧,并且参数类型要匹配
	FUNTION(1, 2);		//调用函数 
	system("pause");

	return 0;
}在这里插入代码片

要点c**加粗样式**
#include <iostream> 
#include "windows.h"
using namespace std;

 typedef void  (* FUNTION)(int ,int);  //有typedef
							
void temp(int x, int y)
{

	cout << x << y << endl;
}
int main()
{	
	
	FUNTION s; //FUNTION当一种指针类型用了
	s=&temp;	//参数类型要匹配
	s(1, 2);		//调用函数 
	system("pause");

	return 0;
}

要点c:有什么用?
先运行代码

在这里插入代码片
//temp.h
#include <iostream>
using namespace std;
void tempAdd(int x, int y)
{

	cout << x <<"   tempAdd    " << y << endl;
}

void tempSub(int x, int y)
{

	cout << x << "   tempSub    " << y << endl;
}
#include <iostream> 
#include "windows.h"
#include "temp.h"

using namespace std;

//要点a,b

typedef void  (* FUNTION)(int ,int);  //有typedef
#define TEMP  tempAdd
 void tempSuccess(FUNTION TEMP, int a, int b)
 {
	 TEMP(a, b);
 }

int main()
{	
	
	//FUNTION s; //FUNTION当一种指针类型用了
	//s=&temp;	//参数类型要匹配
	//s(1, 2);		//调用函数 
	tempSuccess(tempAdd, 1, 2);   //1 tempAdd 2
	tempSuccess(tempSub, 1, 2);   //1  tempSub 2
	system("pause");

	return 0;
}

可以假设 tempSuccess 第1个参数是算法,第2,3个参数是数据的区间,
第一个参数可以是各种算法 如:排序,插入等等

业余人员,高兴就点个赞

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值