栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++函数指针、回调函数

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++函数指针、回调函数

示例:
typedef int (*a) ();
int foo(){}
a f = foo;

则

(*f)();
等价于
foo();

typedef int (*a) ();定义参数为空,返回值为int类型的函数指针类型a,常用于在回调中使用,等价于

typedef int (*) foo;

去掉修饰符typedef和别名,则为int (*)()类型,前面为返回值,后面为输入参数。int (*)(int,int)则表示输入参数为int,int类型,返回为int类型的函数指针,可以用于指向具有相同返回类型和输入参数的函数。因此回调函数可以使没有返回值的函数产生返回值。

例如:

#include "stdafx.h"
#include 
#include 
using namespace std;

typedef int(*a) ();

int hello()
{
	printf("hellon");
	return 1;
}

int world()
{
	printf("worldn");
	return 0;
}

void IuseCallBack(a callback)
{
	int retval = callback();
	printf("%d", retval);
}


int main()
{
	IuseCallBack(hello);
	system("pause");
}

使用IuseCallBack(hello)或IuseCallBack(world)可以打印出"hello"或"world"。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1010192.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号