On 21 Dec 2004 14:43:13 GMT, Hans-Bernhard Broeker
Post by Hans-Bernhard BroekerPost by Active8Post by Rene TschaggelarA 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.
Post by Active8The 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.
Something has to be declared or it won't compile. I know, that's not
a definition, just a generic function pointer. As you go on, it
becomes more clear what you're trying to say (I practiced this same
excersize with Kevin :) ) so don't feel compelled to comment on all
my comments.
Post by Hans-Bernhard BroekerMore importantly, different calls to
'foo' may want to use different functions 'bar', at least some of
which aren't written yet.
That's called function overloading and another way is polymorphism.
You can't *link* a function call to a function with no definition,
If it's *declared" in a header or somewhere "in scope", it will
*compile* but if the definition hasn't been written, the linker
won't find it's entry point in any modules and it won't *link*. But
that's not a generic function pointer I'm talking about.
IOW, you can't assign a *valid* pointer at runtime unless the
function is defined somewhere.
pfunc = atan;
won't work if the linker can't find atan.
pfunc = NULL;
pfunc = atan;
function(pfunc);
groovy, now hit "execute".
Post by Hans-Bernhard BroekerSo 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'.
As I said, I can pass any bogus pointer in a function. The fact is,
that function must have been defined (implemented) somewhere or it
won't link. At design time, not run time. You can grab a valid
pointer at runtime, but there *will be no valid pointer unless the
function was implemented and compiled in a module that the linker
can see.*
Period.
Post by Hans-Bernhard BroekerThe 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.
None of this changes the fact that it won't link. Won't even compile
without a declaration.
function(void){}
void (*pfunc) ();
function(pfunc);
This will compile and link, but if you run this code, you have to
assign a value (address) to the function pointer pfunc and you
can't get that address if it's stilll vaporware.
None of this changes the fact that this ain't the definition of a
callback function.
This is funny. I just typed __define callback function__ into
google and didn't get any returns about phones.
http://gethelp.devx.com/techtips/cpp_pro/10min/10min0300.asp
<quote>
A callback function is one that is not invoked explicitly by the
programmer; rather the responsibility for its invocation is
delegated to another function that receives the callback function's
address.
</quote>
Period.
Post by Hans-Bernhard BroekerPost by Active8Even 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.
where's the linker gonna get the damn valid pointer from, it's ass?
Post by Hans-Bernhard BroekerBut 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.
Good. Now click "Execute". Oh. You're waiting on another group to
implement your function? So you wrote what's known to me as a
"stub". You could just as easily write an empty function if you
don't need the flexibility of function pointers.
say
func(){}
function(func);
and the function "function" will get the pointer to func and execute
it as a "callback". It's still a callback regardless of whether it's
implemented.
-
Best Regards,
Mike