callback functions

Started by
3 comments, last by simeygrimey 20 years, 8 months ago
Can someone explain to me what callback functions are and how to write them? thx
Advertisement
the most common call back funtion is the windows messenger handler function. you pass the address of the function that you wrote to windows and windows calls that function back as it needs to. a simple windows messenger handler looks something like this.
LRESULT CALLBACK WndProc(HWND hWnd, UINT message,    WPARAM wParam, LPARAM lParam){   switch(message)   {   case WM_DESTROY:      PostQuitMessage(0);      return 0;   case WM_SIZE:	   GetWindowRect(hWnd, &windowRect);	   return 0;   default:      return DefWindowProc(hWnd, message, wParam, lParam);   }} // WndProc


windows calls this function whenever there is a message in the queue and handles the message depending on your switch case statements. any messages that you did not handle you can pass to the default message handler function.
"The human mind is limited only by the bounds which we impose upon ourselves." -iNfuSeD
Note that callbacks are a C thing; avoid using them in C++, except to maintain compatibility with C interfaces (such as the windows API). A better solution in C++ is virtual subclasses used as interfaces; check any decent OOP textbook for examples of this.

How appropriate. You fight like a cow.
A callback is just a function pointer. Do some research on function pointers.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
http://www.function-pointer.org

it is a nice site and has helped me with my callback problem
take a look.

http://www.function-pointer.org/CCPP/callback/callback.html#chapter3

Lazzar

if god gave us the source code, we could change the world!
---------------------------------------------------------if god gave us the source code, we could change the world!

This topic is closed to new replies.

Advertisement