一、函数指针三种定义方法
函数名本质就是函数指针,函数实际上就是返回的是函数指针
//函数指针
#include <iostream>
using namespace std;
void func(int a){
cout << "hello world" << endl;
}
int main(){
//函数指针 三种定义方法
//一、先定义函数,再通过类型定义函数指针
typedef void(FUNC_TYPE)(int);
FUNC_TYPE *pFunc = func;
pFunc(1);
//二、定义出函数指针类型,通过类型定义函数指针变量
typedef void( * FUNC_TYPE2)(int);
FUNC_TYPE2 pFunc2 = func;
pFunc2(1);
//三、直接定义函数指针变量
void(*pFunc3)(int) = func;
pFunc3(1);
return 0;
}
hello world
hello world
hello world
二、函数指针的数组
//函数指针的数组
#include <iostream>
using namespace std;
void fun1(){
cout << "func1调用" << endl;
}
void fun2(){
cout << "func2调用" << endl;
}
void fun3(){
cout << "func3调用" << endl;
}
int main(){
void(*pArray [3])(); //语法:[] 写在()里
pArray[0] = fun1;
pArray[1] = fun2;
pArray[2] = fun3;
for(int i=0; i<3; i++){
pArray[i]();
}
return 0;
}
func1调用
func2调用
func3调用
三、函数指针和指针函数的区别
函数指针:指向函数的指针
指针函数:返回值是指针的函数
四、QT中的函数指针
QT中的函数指针 ,等号右边必须严格进行类型转换,转换成与等号左边一样的类型,如15行.
14行是C++中的写法,在QT中不可以的.
图中为函数指针的声明方式