LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-19-2006, 05:18 PM   #1
Hexstatic
LQ Newbie
 
Registered: Nov 2005
Distribution: Slackware 10.2 (kernel 2.6.16)
Posts: 11

Rep: Reputation: 0
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.
 
Old 09-19-2006, 05:21 PM   #2
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
1. Use callback functions, it looks like ye be trying ta use callback methods.
2. Don't use casts, just write th' function prototype th' way it likes.
3. Use [code] tags ta preserve indentation.
 
Old 09-21-2006, 05:10 AM   #3
freegianghu
Member
 
Registered: Oct 2004
Location: somewhere in the street
Distribution: Window$
Posts: 192

Rep: Reputation: 30
There are two ways to resolve:

1. Modify callback function of Audio to static
2. Add addition C-style callback function to call audio's callback

Cheers,
GH
 
Old 09-26-2006, 05:58 PM   #4
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
You should precede the function name with '&'; I don't think conversion to pointer is automatic when casting. You should do that anyway, actually, because it's good form, and you can also use function references in C++ (meaning you should make your code explicit.)
ta0kira
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Matrix of function pointers kvtournh Programming 9 03-20-2006 04:38 AM
problem with function returning pointers ***func() mlaich Programming 5 01-19-2006 09:17 PM
Dynamic array of function pointers - syntax needed seriesx4 Programming 6 01-05-2006 07:45 AM
Function pointers, calling, and templates The_Nerd Programming 2 10-09-2005 07:08 PM
Function pointers in C. Matir Programming 2 03-15-2005 03:10 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:45 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration