Skip to main content
gamedev.net
🔒 Locked

Pointers to Functions..Callback?

Started by JoshG Dec 30, 2001 at 5:23 AM 1 replies 350+ views
Original Post
JoshG
JoshG
Hello all, How do I go about making a function that I can pass a pointer to of another function. I think this kind of thing is called callback functions. I dunno though. The kind of function I am trying to create would be something like the ''CreateThread" fuction, where you have to pass a pointer to a function that later will be called. How do I go about writing a function like this? So far I have: //------------CODE------------ typedef void (*Function)(); Function myFunction; void runFunction(Function pFunction){ myFunction = pFunction; (myFunction)(); } void TheFunctionToRun(){ ShowMessage("Hello"); } void main(){ runFunction(&TheFunctionToRun); } //---------------------------- I know i should simply fun "pFunction" streight away instead of assigning it to a variable, But that is part of the requirement of the function. That code does not work, and I''m not suprised as I have never tried to do anything like this before. Can anyone enlighten me as to how I should fix this problem? BTW, I have seen posts in these forums with sections that have sections for code, and there is syntax highlighting. How do I do this?
kvh
kvh
well, the code works for me, it would help if you told us what problems you are having (error messages please)

more info about function pointers: www.function-pointer.org

read about formatting in the forum faq



Edited by - kvh on December 30, 2001 9:43:32 AM
JoshG
JoshG
Ah,
Sorry about that, I didn''t expect the code to be right, I thought the problem would be obvious as I have never done this before and was bound to make mistakes.

Anyways, the error that the compiler gives is...
Member function must be called or it''s address taken.

I just realised what was causing this error though, I had "()" after the definition of the "MyFunction" variable.

Sorry to have wasted your time

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.