http://www.codeguru.com/cpp/cpp/cpp_mfc/callbacks/article.php/c10557/Callback-Functions-Tutorial.htm

为什么call back函数有用?比如,在实现一个库函数的时候,库函数内部需要调用客户端程序员所写的函数。但库函数没办法知晓客户端函数的具体内容是什么。这时,库函数可以在函数内部用一个指代客户端程序的东西来代替库函数想用的函数,这个指代的东西可以称作prototype, 这个prototype可以作为库函数的参数。 这种库函数定义时用prototype,而prototype的实现交给客户端程序员的方案,叫做callback.

Introduction

If you are reading this article, you probably wonder what callback functions are. This article explains what callback functions are, what are they good for, why you should use them, and so forth. However, before learning what callback functions are, you must be familiar with function pointers. If you aren't, consult a C/C++ book or consider reading the following:

What Is a Callback Function?

The simple answer to this first question is that a callback function is a function that is called through a function pointer. If you pass the pointer (address) of a function as an argument to another, when that pointer is used to call the function it points to it is said that a call back is made.

Because they uncouple the caller from the callee. The caller doesn't care who the callee is; all it knows is that there is a callee with a certain prototype and probably some restriction (for instance, the returned value can be int, but certain values have certain meanings).

If you are wondering how is that useful in practice, imagine that you want to write a library that provides implementation for sorting algorithms (yes, that is pretty classic), such as bubble sort, shell short, shake sort, quick sort, and others. The catch is that you don't want to embed the sorting logic (which of two elements goes first in an array) into your functions, making your library more general to use. You want the client to be responsible to that kind of logic. Or, you want it to be used for various data types (ints, floats, strings, and so on). So, how do you do it? You use function pointers and make callbacks.

A callback can be used for notifications. For instance, you need to set a timer in your application. Each time the timer expires, your application must be notified. But, the implementer of the time'rs mechanism doesn't know anything about your application. It only wants a pointer to a function with a given prototype, and in using that pointer it makes a callback, notifying your application about the event that has occurred. Indeed, the SetTimer() WinAPI uses a callback function to notify that the timer has expired (and, in case there is no callback function provided, it posts a message to the application's queue).

Another example from WinAPI functions that use callback mechanism is EnumWindow(), which enumerates all the top-level windows on the screen. EnumWindow() iterates over the top-level windows, calling an application-provided function for each window, passing the handler of the window. If the callee returns a value, the iteration continues; otherwise, it stops. EnumWindows() just doesn't care where the callee is and what it does with the handler it passes over. It is only interested in the return value, because based on that it continues its execution or not.

However, callback functions are inherited from C. Thus, in C++, they should be only used for interfacing C code and existing callback interfaces. Except for these situations, you should use virtual methods or functors, not callback functions.

Callback Functions Tutorial相关推荐

  1. A Scala Tutorial for Java Programmers

    A Scala Tutorial for Java Programmers English Español 한국어 Deutsch By Michel Schinz and Philipp Halle ...

  2. Glade3 tutorial in chinese

    Part 1 Quick Overview of GTK+ Concepts (转) 如果你没有任何 GTK+ 的编程经验, 那么, 对于我将要阐述的一些概念你也许会听着犯迷糊.不过,不用担心, 在遇 ...

  3. 以太坊去中心化_开发以太坊去中心化投票应用程序的指南

    以太坊去中心化 by Timothy Ko 蒂莫西·高(Timothy Ko) 开发以太坊去中心化投票应用程序的指南 (A guide to developing an Ethereum decent ...

  4. 使用一些我喜欢的东西开始使用ES6

    by Todd Palmer 托德·帕尔默(Todd Palmer) 使用一些我喜欢的东西开始使用ES6 (Getting started with ES6 using a few of my fav ...

  5. 使用Minions解释JavaScript回调

    by Kevin Kononenko 凯文·科诺年科(Kevin Kononenko) 使用Minions解释JavaScript回调 (JavaScript Callbacks Explained ...

  6. rxjava教程_RxJava教程

    rxjava教程 In this tutorial, we'll be introducing you to Reactive Programming and the library RxJava. ...

  7. android生命周期_Android活动生命周期– 7个阶段和功能

    android生命周期 Activity is one of the primary components of an Android application. In this tutorial, w ...

  8. jquery 弹出窗口_jQuery弹出窗口和工具提示窗口动画效果

    jquery 弹出窗口 In this tutorial, we are going to discuss about a jQuery plugin for responsive and acces ...

  9. 工作流 节点子线程_节点JS体系结构–单线程事件循环

    工作流 节点子线程 Today we will look into Node JS Architecture and Single Threaded Event Loop model. In our ...

最新文章

  1. 差异数据库的还原方法
  2. IOS UITableView 加载未知宽高图片的解决方案
  3. 拒绝了对对象 'XXX' (数据库 'XXX',架构 'dbo')的 SELECT 权限
  4. 深度学习核心技术精讲100篇(八十一)-NLP预训练模型ERNIE实战应用案例
  5. JVM学习01总体概述
  6. 使用DOM4J读取和维护XML数据
  7. vs2010 出错:error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
  8. WINDOWS下与LINUX下写C程序的区别
  9. getresourceasstream 路径_Java 获取资源文件路径
  10. mysql做主从分离后插入更新过慢
  11. Java的switch case 语句
  12. 高通android刷机工具,步步高工具高通版刷机救砖教程图解
  13. higtech软件的使用
  14. XXL-JOB任务调度
  15. 【Java集合】Java集合一览(汇总)
  16. r语言C指数的置信区间,R语言入门:均值置信区间
  17. face_recognition人脸识别框架
  18. Html5禁止浏览器后退,多页面网站禁用浏览器后退键
  19. Android中相机(Camera)画面旋转角度分析:手机摄像头的“正向”、手机画面自然方向、相机画面的偏转角度
  20. react中findDOMNode

热门文章

  1. java并发初探ReentrantWriteReadLock
  2. Python学习day07 - Python进阶(1) 内置方法
  3. 用实例的方式去理解storm的并发度
  4. [18/11/23]面向对象和面向过程
  5. 2018.09.19 atcoder AtCoDeer and Election Report(贪心)
  6. Java根据出生年月日获取到当前日期的年月日
  7. C#进行Visio二次开发之Web端启动绘图客户端并登录
  8. java多线程系列:ThreadPoolExecutor源码分析,java基础面试笔试题
  9. Spring 的application.properties项目配置与注解
  10. 如何设置jquery的ajax方法为同步