Path: chuka.playstation.co.uk!news From: Andrew Partington Newsgroups: scee.yaroze.freetalk.english Subject: Using C++ with C function pointers Date: Tue, 19 Sep 2006 22:13:55 +0000 Organization: PlayStation Net Yaroze (SCEE) Lines: 78 Message-ID: NNTP-Posting-Host: spc1-ward1-0-0-cust330.bagu.broadband.ntl.com Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.13) Gecko/20060422 X-Accept-Language: en-gb, en, en-us, ja 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)' 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' Any C++ gurus there who can help? Cheers, Andy P 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; ... }