Using C function pointers in C++
Hi all,
I'm trying to use C-style callback functions from within C++, but i'm not having much luck.
Specifically, i'm using SDL_mixer, and I want to be able to pass my callback function to Mix_ChannelFinished so I can tell when its OK to play another sample on a channel once it's finished.
I looked about on the web, and read that you can cast the function pointer to the type that the C function is expecting like so:
Mix_ChannelFinished((void (*)(int)) myCallbackFunction);
Still can't get it to compile though, I get the following error message instead:
Audio.cpp:106: error: argument of type `void (Audio::)(int)' does not match ` void (*)(int)'
Strange, since the cast should sort that out I would have thought. Fine, says I, i'll make the callback a static method. Now, this works if I comment out anything that touches the flag, but if I try to use the flag inside the (now static) callback, I get this error:
Audio.cpp:162: error: invalid use of member `Audio::chDone' in static member function
Making the boolean flag static produces this error at every point it is used in the code (doesn't matter if I use the scope resolution operator or not):
/home/apartington/source/c++/gg-sdl/gravsdl/src/Audio.cpp:108: undefined reference to `Audio::chDone'
Can anyone help?
Cheers,
Hexstatic
P.S.: Code looks something like this...
//Class definition in Audio.h
class Audio
{
...
...
void someMemberFunction();
void myCallbackFunction(int i);
bool flag;
...
...
};
//Implementation in Audio.cpp
Audio::someMemberFunction()
{
...
...
Mix_ChannelFinished((void (*)(int)) myCallbackFunction);
...
...
}
Audio::myCallbackFunction(int i)
{
...
flag = true;
...
}
Last edited by Hexstatic; 09-19-2006 at 05:19 PM.
|