Advertisement

C++ Question.. not really openGL related

Started by August 29, 2001 02:09 AM
1 comment, last by akudoi 23 years, 5 months ago
my question is. how can i call a function thats held in a variable? or basically that. the reason for this is a basic scripting language/console im making for my opengl game. here is what i was thinking of in terms of code. #include void test(); char* buffer = "test()"; main() { ??????????????????? } test() { printf("testing"); } now how would i call test() using buffer? ive seen void (*func)(); in the odd code ive came accross, however i havent a clue what to do with it. [im assuming that its what calls the function from the variable] i dont want to do it with a lot of if''s or switch''s, the command list will be quite large. so this would be ideal. Thanks Very Much for any help. -Later
It is impossible to call a function by the name presented within a pointer to char buffer. However, I have a solution. Perhaps you are developing a menu system similiar to this one:

tart<br>[O]ptions<br>[Q]uit<br>:<br><br>The user enters S, O or Q for their desired choice. However, pretend you have a special build for your game and a normal build that are isolated in two seperate functions.<br><br>What we could do is define pointer to functions for each menu option, and just call this function pointer. Like this:<br><br>void (*pfnStart)(void);<br>void (*pfnOptions)(void);<br>void (*pfnQuit)(void);<br><br>Here would be our two start functions to choose from:<br><br>void DebugStart(void);<br>void BuildStart(void);<br><br>Now suppose when the user presses S to start we want to run the DebugStart function. All we do is assign the address of the DebugStart function to our pfnStart pointer to function:<br><br>pfnStart = DebugStart;<br><br>We can now use the function call notation of pfnStart() as a synonym for the direct function call DebugStart. This allows us to minimize code rewriting, and can be really useful.<br><br>Not what you asked for, but hope it can be of some use. Good luck.
"The time has come", the Walrus said, "To speak of many things."
Advertisement
Well, the reason for doing so is so that your functions don't have to be gigantic if-elseif-elseif statements. Or at least, that's the reason I found for mine...

In your way, the ,[O],[Q] way, you could easily write it like this:<br><br><b>void iGotAKeyPress(char keypress)<br>{<br>if(keypress == 'S') goDoFunction1();<br>else if(keypress == 'O') goDoFunction2();<br>else if(keypress == 'Q') goDoFunction3();<br>} </b> <br><br>That's all great and good, but if your if-block is lengthy, it could result in some slowdown.<br><br>The way around it is to pass a pointer to the FUNCTION you want it do it, like so:<br><br><b>void iGotAKeyPress(void (*f)()); // Function prototype </b> <br><br>What this relates is this: the variable <i>f </i> is a pointer to a function. The void in front refers to the return-type of <b>f </b> , and the parenthesis after it refer to the parameter list of <b>f </b> , so theoretically you could also do<br><br><b>void iGotAKeyPress(double (*f)(int, double, string)); </b> <br><br>(Note: The reason for the parenthesis around <b>*f </b> is so that we don't interpret it as <b>void* f() </b> , since C++ can't tell the difference between <i>void* f() </i> and <i>void *f() </i> .)<br><br>So now, the cheap way to do write the function is this:<br><br><b>void iGotAKeyPress(void (*f)())<br>{<br>if(f != NULL) f();<br>} </b> <br><br>Now to call the function "iGotAKeyPress", you'll need to pass in as the parameter the NAME of the function you wish to run. So if "goDoFunction1()" is the function that starts the game, simply call:<br><br><b>iGotAKeyPress(goDoFunction1) </b> <br><br>(Note: the function passed cannot me a class function.)<br><br>Now, I know the example given isn't that logical, but this style becomes really useful if you have two functions that are almost identical save for one central area of the code where they're different (also if you have a big function that has a small if/else(if) block at the end). In that case, separate out all the code that's different into two smaller functions. Then, your big code will get down to<br><br><b>void bigFunction(void (*f)())<br>{<br>// All the stuff that always happens goes here<br>if(f) f(); // <– Calls the appropriate section of "different" code…<br>// Anything else I want to do<br>} </b> <br><br>I hope that explains it to you! <img src="wink.gif" width=15 height=15 align=middle><br><br>~ Dragonus <img src="wink.gif" width=15 height=15 align=middle><br><br>Edited by - Dragonus on August 29, 2001 9:42:41 AM

This topic is closed to new replies.

Advertisement