Call back routine

前端 未结 6 579
野的像风
野的像风 2021-02-01 10:14

In the Learning OpenCV book, I came to the term callback, and sometimes used with routine as callback routine.

What d

6条回答
  •  悲&欢浪女
    2021-02-01 10:57

    "Routine" in this context is the same as "function". The term goes back to older languages (like Fortran) that made a difference between functions, that returns values, and (sub)routines that don't.

    "Callback" is a technique where you provide a pointer to one of your functions ("routines") to the system/API/framework and the system/API/framework would call it back when it feels like doing so. So a callback routine, or simply a callback, is a function that's intended for such usage.

    In strictly object languages (like Java) they typically use listeners and delegates for that. The callback technique, in its C++ form, has the advantage that's it's compatible with non-object-oriented languages like classic C.

    EDIT: in the Microsoft C run-time library, this technique is used for qsort() function. The compare argument is a function pointer to a callback routine. It's called by the RTL whenever two array elements need to be compared. It's not a typical example 'cause all the calls to compare happen before the qsort() call returns.

    In Win32 API, callbacks are a staple. The window procedure is a prime example - you pass a pointer to it in the WNDCLASS structure, the system calls the procedure back as the message arrive. In this case, the callback routine is invoked long after the RegisterClass() - for the whole lifetime of the window.

    In POSIX/Unix/Linux, the signal processing function is an example. See the signal() syscall description.

提交回复
热议问题