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.