|
-
Nov 18th, 2001, 09:50 AM
#2
Callback functions are an old concept. They are functions that are not called by the author himself but by another function, for whatever purpose. This is realized via function pointers (pointers that don't contain the address of data, but rather of code).
Callback functions can be used for many thinks: enumerations (since it is complicated to return arrays, these functions are called once for each element of a list), to handle specific computations (there are array sort functions that call a callback to compare two entries of the array), or to handle messages (WNDPROC).
If you have the need, you can do that yourself. You first need to declare a function pointer type:
Code:
typedef int (*MYFUNCPTR)(int, int);
// declare a pointer:
MYFUNCPTR ptr;
Notice the bracket around the asterisk and the name - they are important!
Then you write a function that takes such a pointer as an argument:
Code:
void arraysort(int* ar, int arsize, MYFUNCPTR pfnComp)
{
...
// call the function
k = pfnComp(i, j);
...
}
Then you write a function you want to supply:
Code:
int MyFunc(int i, int j)
{
return i*j;
}
And finally call the function:
Code:
arraysort(ar, ARRAY_SIZE, MyFunc);
No brackets after the function name - you want the address, not the return value.
The CALLBACK in windows is just an alias for __stdcall, which is a calling convention. You're not concerned with it unless you write in assembly. Just remeber never to mix calling conventions: there are __cdecl, __stdcall and __thiscall (C++ member functions).
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|