Call back functions

Hi guys,

So I know/understand the basic premise behind call backs a function will take a function pointer as it's argument, and when a particular event happens that function will be called, such as if a key is pressed an interrupt will be sent and the callback function will be called, the windows API has many call back functions, QT and other GUI frameworks also have call back functions,

so basically how does this happen? is multi threading used?? obviously when we wait for an event to happen lets say a mouse click our callee function that deals with the mouse click which then calls the callback function, surely this callee function isn't repeatedly in a while loop waiting for an event? surely a thread is created which handles when this mouse is clicked? for example in QT when we click a button a function gets called.

Reading Bjarnes practices and principles he mentions with call backs the call back function has to be static as to not belong to an object, and return nothing, why is this?

for example in Bjarnes book on page 543 and 545 Bjarne creates a window class which inherits from an FLTK window, this window class contains a button called next_button so the call back function will be called when the button is pressed.

let me post the code

could somebody further elaborate on what Bjarne is trying to explain, he is basically saying that the reason why the function has to be static and return void is so that other languages can also use the API provided by the OS, I'm not completely sure what he is trying to convey. why would it need to be static and what is meant by address in the arguments? he says you can't use c++ references as the OS or compiler does not know what they are (sp then what would you use?)... pretty confused

thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18


Simple_window::Simple_window(Point xy,int w,int h,const string& title)
: Window(xy,w,h,title),next_button(Point(x_max()-70,0),70,20,"next",cb_next), button_pushed(false){


  attach(next_button);

}

// call back function

void Simple_window::cb_next(address,address pw){

      reference_to<Simple_window>(pw).next();

}







Last edited on
so basically how does this happen? is multi threading used?? obviously when we wait for an event to happen lets say a mouse click our callee function that deals with the mouse click which then calls the callback function, surely this callee function isn't repeatedly in a while loop waiting for an event? surely a thread is created which handles when this mouse is clicked? for example in QT when we click a button a function gets called.


No, multi-threading isn't required in this scenario. Calling via function pointer is not much different than calling any standard function; it's just a function call.

In any GUI system, events are packaged as messages (just some numeric information). The main loop of your program is called with that message data, and eventually the GUI system searches through a container to see what function should be called, forwarding that data. There's no loop waiting to call that function - the loop is the message loop common to most GUI systems. That loop is called as an event handler object, usually a window's message loop. No thread required.

In Stroustrup's book, around the chapter I think you're referencing, an introduction to callbacks is presented with a simple call by function pointer, which by definition can't be a member function. In order to call back a member function the instance of the object upon which the function is to be called must be provided, but a simple callback by function pointer has no pointer to an instance. Hence, the function must be either a stand alone (non-member) function, or it can be a static function in class membership.

Stroustrup shows how the data, unpacked by the framework, provides a pointer to an instance (pw), which is then used to make a call to a member function. It isn't about other languages, it is about how a simple callback specifier is limited to non-member or static functions. The return type for a callback can be non-void, but for most event responders there may be limited utility in a return value.

If you proceed through the rest of the material, it eventually gets expanded.


Topic archived. No new replies allowed.