From where is the initialize function in text.c called?

Scott Tringali scott.tringali at totalviewtech.com
Mon Aug 18 15:37:48 CEST 2008


Initialize is indeed a callback.  When you create a widget of a 
particular class, it will initialize that the widget.  This is pretty 
easy to demonstrate.  Load up nedit under a debugger, set a breakpoint 
on Initialize, and you'll see that's it's called from deep inside the 
Motif library.

Functions in C can be stored and called through function pointers. 
Putting Initailize into a structure doesn't call it right then - it just 
stores the function pointer there.  Later on the library code will call 
the function with the appropriate parentheses.

The whole point of callbacks is that you only need to make sure the type 
signatures match, and that's the responsibility of the callee. There's 
no way Xt can #include "text.h" since it's compiled before nedit even 
exists.  But Xt can call any category of functions that has that has a 
particular function signature.  We simply register the particular 
function with it (via this structure), it knows how to call functions of 
that type, and there you go.  It has no idea *which* function we are 
registering.  We could deign to supply a function that took the same 
signature and did absolutely nothing and it could not tell the difference.

A lot of the complexity you're seeing here isn't C, but a the reality of 
any sort of user interface code.  Every UI needs callbacks or a some 
sort of abstract signal/slot mechanism.



More information about the Develop mailing list