Re: what's a callback?

From: Hans-Bernhard Broeker (broeker_at_physik.rwth-aachen.de)
Date: 12/21/04


Date: 21 Dec 2004 14:43:13 GMT

In comp.arch.embedded Active8 <replyt2group@ndbbm.net> wrote:

> >> > "Rene Tschaggelar" <none@none.net> wrote in message
> >> > news:41c808c4$0$30029$5402220f@news.sunrise.ch...
> >> >>A callback is a function call to a fuction that at design
> >> >>time does not yet exist.

> Not even remotely true, let alone possible to compile.

It's actually a lot more true than you believe. Rene may have
condensed its essence down to a little too few words for you to
recognize it, but he did get it right.

> The function *must* exist, however you can select a function and
> pass a pointer at runtime.

> You can't even compile a function in C++ without an implementation.

Correct, but that's not the issue at hand. We're talking about the
not-yet-existing implementation of a _called_ function, not its
caller.

To rephrase Rene's statement: a callback is what you use if a function
'foo' needs to be written now, which has to call another function
'bar' that may not exist yet. More importantly, different calls to
'foo' may want to use different functions 'bar', at least some of
which aren't written yet.

So you declare the interface signature of 'bar', and make a pointer to
a function of that signature (or, in OO, an object implementing that
interface) part of the set of arguments passed to 'foo'.

The central aspect that makes this a good idea (compared to other
methods of selecting a function to call from a collection, e.g. an
index into a table of function pointers, or a switch() between a lot
of function calls), is indeed, as Rene pointed out, that it lets you
split up "compile time" into at least two separate phases, to be
carried out by different people, at different places and times: one is
the compilation time of 'foo', the other the (potentially lots of)
compilations of 'bar', and also of the function that calls 'foo' and
passes 'bar' to it.

> Even an empty function is an *implementation* of a function.

But an empty function is not part of this design --- there's just the
prototype, and a *pointer* to such a function. But no actual
implementation of any such function is involved in this design pattern
named "callback". That's how you can implement the pattern, without
having any implementation of the actual callback function at hand.

-- 
Hans-Bernhard Broeker (broeker@physik.rwth-aachen.de)
Even if all the snow were burnt, ashes would remain.


Relevant Pages

  • Re: whats a callback?
    ... Rene may have ... 'foo' needs to be written now, which has to call another function ... 'bar' that may not exist yet. ... So you declare the interface signature of 'bar', and make a pointer to ...
    (comp.arch.embedded)
  • Re: "Mastering C Pointers"....
    ... > Windows shortcut, there is definitely an analogy there. ... a pointer variable holds an address of an object. ... int foo = 12; ... int *bar; ...
    (comp.lang.c)
  • Re: Interview questions
    ... and note that this means the implementor of Bar needs to ... Bar might want to inherit some virtual methods of Foo. ... is the first member of bar, we can say a Bar "is a" Foo. ... > You cannot pass a pointer to an object of type Bar ...
    (comp.lang.cpp)
  • Re: Circular dependency - I think..
    ... > I see that you are not initialising the pointer to BAR contained in FOO. ...
    (comp.lang.cpp)
  • Re: Getting the instance pointer from a called function
    ... >> pointer) which called bar(), from inside bar, when that happens. ... store a pointer to the current instance of Foo in ... will store pointers of Foo instances, but barhas a strict prototype and ...
    (comp.lang.cpp)