int (*callback)(int index, int item, void *param)
int (*callback)(int index, int item, void *param)
To copy to clipboard, switch view to plain text mode
This is a signature of a callback function pointer returning an int and taking two ints and a void*.
If I'm correct you could do the following:
typedef int (*callback)(int index, int item, void *param) CALLDEF;
void dummy(){
}
found = traverseWith(array, length, (CALLDEF)dummy, &index);
typedef int (*callback)(int index, int item, void *param) CALLDEF;
void dummy(){
}
found = traverseWith(array, length, (CALLDEF)dummy, &index);
To copy to clipboard, switch view to plain text mode
I cast the functor to a type the "traverseWith" function expects so the compiler shouldn't interfere. And when you call the above line, the compiler will create a frame stack for a method that expects a return value and has three arguments. But the function which actually is called expects a completely different frame stack, which will lead to stack corruption and a crash.
In C++ you'd use function objects based on templates, which do type checking, etc. making it a bit safer (you pass around objects and not pointers). Qt uses a simmilar approach (for example when sorting).
Bookmarks