RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
【Example】C++ 回调函数及 std::function 与 std::bind

回调函数是做为参数传递的一种函数,在早期C样式编程当中,回调函数必须依赖函数指针来实现。

创新互联公司-专业网站定制、快速模板网站建设、高性价比东川网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式东川网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖东川地区。费用合理售后完善,10年实体公司更值得信赖。

而后的C++语言当中,又引入了 std::function 与 std::bind 来配合进行回调函数实现。

标准库中有大量函数应用到了回调函数,其中 std::sort 就是一个经典例子。

一,回调函数

回调函数的创建步骤大概为:

1,声明一个函数指针类型。

2,拟写使用回调函数的函数,将函数指针类型及变量名声明作为参数传递。

3,拟写符合函数指针类型的实现函数,将实现函数的指针作为参数传递给使用它的函数。

下面演示了一个最简单的回调函数定义及使用:

typedef int (*Calc)(int a, int b);
int CalcValue(int a, int b, const Calc &func) {
return func(a, b);
}

int Add(int a, int b) {
return a + b;
}

int main()
{
int a = 4;
int b = 6;
int c = CalcValue(a, b, Add);

    std::cout<< "Value:" << c << std::endl;
return EXIT_SUCCESS;
}

本文题目:【Example】C++ 回调函数及 std::function 与 std::bind
地址分享:http://scyingshan.cn/article/dsogocd.html