How to explain callbacks in plain english? How are they different from calling one function from another function?

后端 未结 30 1937
时光说笑
时光说笑 2020-11-22 11:13

How to explain callbacks in plain English? How are they different from calling one function from another function taking some context from the calling function? How can thei

30条回答
  •  不要未来只要你来
    2020-11-22 11:35

    Always better to start with an example :).

    Let's assume you have two modules A and B.

    You want module A to be notified when some event/condition occurs in module B. However, module B has no idea about your module A. All it knows is an address to a particular function (of module A) through a function pointer that is provided to it by module A.

    So all B has to do now, is "callback" into module A when a particular event/condition occurs by using the function pointer. A can do further processing inside the callback function.

    *) A clear advantage here is that you are abstracting out everything about module A from module B. Module B does not have to care who/what module A is.

提交回复
热议问题