**要点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个参数是数据的区间,
第一个参数可以是各种算法 如:排序,插入等等
业余人员,高兴就点个赞