#include <iostream>
//定义一个无返回值的指针函数类型hs,包含一个int型的参数
typedef void (*hs)(int);
using namespace std;
void Sub_Print(int i)
{
cout<<(i-1)<<" ";
}
void Add_Print(int i)
{
cout<<(i+1)<<" ";
}
void Just_Print(int i)
{
cout<<i<<" ";
}
void Call(const char *name,hs func)
{
int array[9] = {1,2,3,4,5,6,7,8,9};
cout<<name<<endl;
for (int i = 0; i < sizeof(array)/sizeof(array[0]); i++)
{
func(array[i]);
}
}
int main()
{
//这里要定义char型的指针数组,数组内的内个元素指向一个字符串
//char names[] = {"输出","减1输出","加1输出"};-->错误定义
char *names[] = {"输出","减1输出","加1输出"};
//函数指针数组
hs fun_sz[] = {Just_Print,Sub_Print,Add_Print};
for (int i = 0; i < 3; i++)
{
Call(names[i],fun_sz[i]);
cout<<endl;
}
cin.get();
return 0;
}
C++函数指针数组
最新推荐文章于 2023-06-01 10:22:56 发布