C / C ++中的函数回调是什么。如何使用它 ? [英] What is the function callback in C/C++ . How to use it ?

查看:74
本文介绍了C / C ++中的函数回调是什么。如何使用它 ?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我正在学习C / C ++程序,但我不了解回调函数。请帮帮我:(。



非常感谢



我尝试了什么:



什么是C / C ++中的函数回调。如何使用它?

解决方案

回调函数是一个函数,它作为参数传递给另一个函数,并由该代码调用以动作它。



有一个很好的解释SO:如何用简单的英语解释回调?它们与从另一个函数调用一个函数有什么不同? - Stack Overflow [ ^ ]

这里解释了主要思想和目的:控制反转 - 维基百科,免费的百科全书 [ ^ ]。



从技术上讲,它可能意味着不同的方式。通常,这是将指向回调函数的指针作为某些API的参数(在上面引用的文章中称为可重用库)函数。例如,它在Windows API中用于UI开发。有一个非常有特色的例子,请看这一个: WindowProc回调函数(Windows) [ ^ ]。



传递给API的函数的作用与的概念有关一等公民。请参阅:一等公民 - 维基百科,免费百科全书 [ ^ ]。



这意味着作为回调传递的函数需要被视为一个对象,与所有其他类型的对象相同。在C和C ++中,它可以像指针一样简单,但在其他技术中使用一些更高级的编程概念,例如委托(委托实例可以传递),接口(实现某些接口的对象通过接口引用传递,而它的编译类型可能是未知的),依此类推。在C ++中,可以通过现有C ++特性(例如类或类模板)对这些概念进行建模/实现。



注意回调,从广义上讲单词,是现代编程的基础之一。如果没有透彻理解概念和技术,任何有效的软件开发都是不可能的。



-SA


在开始使用CallBacks之前,你需要了解函数指针。



所以如果我写这样的东西

 int(* foo)(int,int); 

int IntAdd(int x,int y){
return x + y;
}

int IntSub(int x,int y){
return x - y;
}

int main(){
int i;
foo =& IntAdd;
i = foo(5,4);
foo =& IntSub;
i = foo(5,4);
}



调试代码并观察发生的情况并比较i的回报。这只是函数指针的一种用法。一旦你开始使用函数指针,你就会明白为什么你会创建回调,这很明显。



你不理解它们的事实意味着你没有使用函数指针。



理解它们的最佳练习是尝试在Ansi C中创建目标代码,没有C ++命令。因此,尝试创建类似于流对象的东西,这是一个相当简单的对象,但仅限于Ansi C.


Hi everyone,

i'm studying C/C++ program but i don't understand about callback function . Please help me :( .

Many thanks

What I have tried:

what is the function callback in C/C++ . How to use it ?

解决方案

A callback function is a function which is passed as an argument to another function and which is invoked by that code to "action" it.

There is a good explanation on SO: How to explain callbacks in plain english? How are they different from calling one function from another function? - Stack Overflow[^]


The main idea and the purpose are explained here: Inversion of control — Wikipedia, the free encyclopedia[^].

Technically, it may mean different ways. Often, this is passing a pointer to a callback function as a parameter of some API (referred as to "reusable libraries" in the article referenced above) function. For example, it is used everywhere in Windows API for UI development. For one very characteristic example, look at this one: WindowProc callback function (Windows)[^].

The role of a function passed to the API is related to the concept of first-class citizen. Please see: First-class citizen — Wikipedia, the free encyclopedia[^].

It means that the function passed as a callback needs to be considered as an object, on par with all other kinds of objects. In C and C++ it can be as simple as the pointer, but in other technologies some more advanced programming concepts are used, such as delegates (delegate instances can be passed), interfaces (objects implementing some interface are passed by the interface reference, while its compile-type can be unknown), and so on. In C++, such concepts can be modeled/implemented through the means of existing C++ features, such as classes or class templates.

Note that callbacks, in wider sense of this word, is one of the basics of modern programming. Without thorough understanding of the concept and techniques, any productive software development would be impossible.

—SA


Before starting with CallBacks you need to understand function pointers.

So if I write something like this

int (*foo) (int, int);

int IntAdd(int x, int y){
	return x + y;
}

int IntSub(int x, int y){
	return x - y;
}

int main(){
	int i;
	foo = &IntAdd;
	i = foo(5, 4);
	foo = &IntSub;
	i = foo(5, 4);
}


Debug the code and watch what happens and compare the returns of i. That is the but one use of a function pointer. Once you start playing around with function pointers you will see why you will create callbacks, it becomes obvious.

The fact you don't understand them means you haven't used function pointers.

The best exercise to do to understand them is try creating object code in Ansi C, no C++ commands. So try creating something like a stream object which is a rather simple object but limit yourself to Ansi C.


这篇关于C / C ++中的函数回调是什么。如何使用它 ?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆