typedef void (*Fun) (void*) 的相关解释

1、一般用法:

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
void test01() {
	int a = 3;
	typedef  int MyInt;
	MyInt b = 2;
	cout << a << "  " << b << endl;
}
int main(void) {
	test01();
	return 0;
}
3  2
请按任意键继续. . .

2、二般用法(做函数指针)

#include <iostream>
using namespace std;

//定义一个函数指针MyFun,它指向一个返回类型为int,有一个整型的参数的函数
int (*MyFun)(int);
int YourFun(int val){
	cout << val << endl;
	return 0;
}
int main(void){
	//将函数YourFun的地址赋值给变量MyFun
	MyFun = YourFun;
	//函数YourFun是函数名也是函数的入口地址,将其赋值给对应的函数指针MyFun,即MyFun也是函数的地址了
	MyFun(2);
	return 0;
}
2
请按任意键继续. . .

3、三般用法(与typedef结合,定义一个函数指针类型)

#include <iostream>
using namespace std;

//定义一个函数指针类型MYFUN,它定义的对象指向一个返回类型为int,有一个整型的参数的函数
typedef int (*MYFUN)(int);
int YourFun(int val){
	cout << val << endl;
	return 0;
}
int main(void){
	//用MYFUN类型定义一个函数指针
	MYFUN MyFun;
	MyFun = YourFun;
	//函数YourFun是函数名也是函数的入口地址,将其赋值给对应的函数指针MyFun,即MyFun也是函数的地址了
	MyFun(3);
	return 0;
}
3
请按任意键继续. . .

4、四般用法(typedef void (*Fun) (void*))

#include <iostream>
using namespace std;

//定义一个函数指针类型MYFUN,它定义的对象指向一个返回类型为void,有一个void*类型的参数的函数
typedef void (*MYFUN)(void*);
void YourFun(void *p){
	//cout << *val << endl;
	cout << *((int*)p) << endl;//void*不能直接解引用
	return;
}
int main(void){
	//用MYFUN类型定义一个函数指针
	MYFUN MyFun;
	MyFun = YourFun;
	//函数YourFun是函数名也是函数的入口地址,将其赋值给对应的函数指针MyFun,即MyFun也是函数的地址了
	int num = 8;
	MyFun(&num);
	return 0;
}
  • 9
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值