C++ Question
What are function pointers?, What are they used for?, And how are the implemented? I can see the use for variable pointers but not function pointers. Don''t you need to use function pointers to access OpenGL''s extensions?
Thanks Alot
~Steve~
Just posted that: my post at http://www.gamedev.net/community/forums/topic.asp?topic_id=59008
To sum up though, function pointers are merely a pointer to a function call, and they''re used all the time with callbacks, especially in GLUT.
As a parameter to a function, you write it like this:
void function(double (*f)(int, int));
where f is the funtion pointer, double is the return type of the function, and (int, int) is the parameter list of the function f .
Within the function, you just run the function as if it were an a normal call.
if(f != NULL) f();
(I check to make sure f is a valid function first.
)
Lastly, now to call the function function , you pass it as a parameter any in-scope function that IS NOT a class member function.
Here''s a quick example:
Hope this helps!
~ Dragonus
To sum up though, function pointers are merely a pointer to a function call, and they''re used all the time with callbacks, especially in GLUT.
As a parameter to a function, you write it like this:
void function(double (*f)(int, int));
where f is the funtion pointer, double is the return type of the function, and (int, int) is the parameter list of the function f .
Within the function, you just run the function as if it were an a normal call.
if(f != NULL) f();
(I check to make sure f is a valid function first.

Lastly, now to call the function function , you pass it as a parameter any in-scope function that IS NOT a class member function.
Here''s a quick example:
|
Hope this helps!
~ Dragonus

I don''t understand how this can reduce code.
if (keypress == ''s'') iGotAKeyPress(goDoFunction1);
if (keypress == ''o'') iGotAKeyPress(goDoFunction2);
Isn''t the parameter that you pass to IGotAKeyPress suppose to be different for each key that can be pressed?
Sorry if i sound a bit stupid, but i''m slightly confused I know that a function pointer is used to point to the entry point of another function, although i am unable to see how this could allow you to load different code for different keys.
Thanks Alot
~Steve~
if (keypress == ''s'') iGotAKeyPress(goDoFunction1);
if (keypress == ''o'') iGotAKeyPress(goDoFunction2);
Isn''t the parameter that you pass to IGotAKeyPress suppose to be different for each key that can be pressed?
Sorry if i sound a bit stupid, but i''m slightly confused I know that a function pointer is used to point to the entry point of another function, although i am unable to see how this could allow you to load different code for different keys.
Thanks Alot
~Steve~
To be able to run different code for different keys using function pointers, you could have an array of function pointers, where each index in the array is the pointer for that key. Sort of like the array to keep track of which keys are pressed that is in NeHe''s base code. Then you just call the function pointer that is in the array location of the given key pressed.
Thus, this:
if (keypress == ''s'') DoSKey();
if (keypress == ''o'') DoOKey();
can be represented like this:
if( fnPtrs[keypress] != NULL )
fnPtrs[keypress]();
I hope that helps!
J.W.
Thus, this:
if (keypress == ''s'') DoSKey();
if (keypress == ''o'') DoOKey();
can be represented like this:
if( fnPtrs[keypress] != NULL )
fnPtrs[keypress]();
I hope that helps!
J.W.
It may not reduce code, but it makes the overall function length smaller. Here''s how I use it:
I''ve got a vehicle. Over time the vehicle moves, and I do this by schedule a list of waypoints for it to travel at a certain velocity.
The GUI is like an editor. It dynamically changes the fields and attributes of the vehicle on the fly. However, sometimes, I want to disable the GUI fields, and other times, I want them active.
This is all fine and good when I disable the screen. It does the same thing everytime for all intensive purposes. (There are some times when it zeroes all the fields out, and sometimes when it retains the contents of them.) So I just use:
void window::disable()
{
// Disable the fields
if(weNeedToZeroTheFields)
{
// Zero the fields
}
}
However, enabling the GUI is trickier. Realizing that sometimes I zero the GUI fields out, I''m going to have to reload them at some point, neh?
So I have to reload the values. Problem. I have a special case: If we''re looking at a vehicle just after it''s been loaded fresh, I want to load a different set of values, and the values don''t even come from the same class. How do I do this?
I could use an if/else statement to do it, but both of the blocks would be extremely long, and we like to keep functions as small as possible. That''s where function pointers come in.
void generalCase() {...}
void specialCase() {...}
void window::enable(void (*f)())
{
// Perform common code
if(f) f();
}
Doing this allows me to do load whatever I want into the GUI through:
win->enable(specialCase);
win->enable(generalCase);
The advantage is that all of the code within the if-else block is moved elsewhere. But the real advantages comes when/if I decide to add a 2nd special case. With if-else, I''ve got to rewrite the block. With function pointers, I just create the function and pass in the appropritate pointer to that function.
This usage of function pointers though, isn''t the real usage of them though. It''s actually more a side-effect from using them. The real purpose of function pointers is as a callback function, which is used heavily within most GUI libraries (Fastlight Toolkit for one) and GLUT.
Hope that explains what I was thinking... ^^O
~ Dragonus
I''ve got a vehicle. Over time the vehicle moves, and I do this by schedule a list of waypoints for it to travel at a certain velocity.
The GUI is like an editor. It dynamically changes the fields and attributes of the vehicle on the fly. However, sometimes, I want to disable the GUI fields, and other times, I want them active.
This is all fine and good when I disable the screen. It does the same thing everytime for all intensive purposes. (There are some times when it zeroes all the fields out, and sometimes when it retains the contents of them.) So I just use:
void window::disable()
{
// Disable the fields
if(weNeedToZeroTheFields)
{
// Zero the fields
}
}
However, enabling the GUI is trickier. Realizing that sometimes I zero the GUI fields out, I''m going to have to reload them at some point, neh?
So I have to reload the values. Problem. I have a special case: If we''re looking at a vehicle just after it''s been loaded fresh, I want to load a different set of values, and the values don''t even come from the same class. How do I do this?
I could use an if/else statement to do it, but both of the blocks would be extremely long, and we like to keep functions as small as possible. That''s where function pointers come in.
void generalCase() {...}
void specialCase() {...}
void window::enable(void (*f)())
{
// Perform common code
if(f) f();
}
Doing this allows me to do load whatever I want into the GUI through:
win->enable(specialCase);
win->enable(generalCase);
The advantage is that all of the code within the if-else block is moved elsewhere. But the real advantages comes when/if I decide to add a 2nd special case. With if-else, I''ve got to rewrite the block. With function pointers, I just create the function and pass in the appropritate pointer to that function.
This usage of function pointers though, isn''t the real usage of them though. It''s actually more a side-effect from using them. The real purpose of function pointers is as a callback function, which is used heavily within most GUI libraries (Fastlight Toolkit for one) and GLUT.
Hope that explains what I was thinking... ^^O
~ Dragonus

Function pointers are good for lots of stuff.
One of Andre LaMothe''s older books used them for multitasking under dos, I remember. You had a multitasking kernel, and you could pass in the functions you wanted it to switch between. That way you didn''t rewrite the kernel everytime you added or removed a new function.
You can also do things like write your AI code in a function, then give all the characters that use that type of AI a pointer to that function. This works nicely if you might want to switch AI functions during the game, and you''d rather not use big case statements on state variables.
In UNIX environments, you can use them to set signal handlers, which can help out with timing and multiprocess programs.
One of Andre LaMothe''s older books used them for multitasking under dos, I remember. You had a multitasking kernel, and you could pass in the functions you wanted it to switch between. That way you didn''t rewrite the kernel everytime you added or removed a new function.
You can also do things like write your AI code in a function, then give all the characters that use that type of AI a pointer to that function. This works nicely if you might want to switch AI functions during the game, and you''d rather not use big case statements on state variables.
In UNIX environments, you can use them to set signal handlers, which can help out with timing and multiprocess programs.
Why do you have to use function pointer''s to use OpenGL''s extensions? Why can''t you just call it as a normal function? How do you call openGL extensions using function pointer''s anyway?
Thanks Alot!!
~Steve~
Thanks Alot!!
~Steve~
quote:
Original post by steveharper101
Why do you have to use function pointer''s to use OpenGL''s extensions? Why can''t you just call it as a normal function? How do you call openGL extensions using function pointer''s anyway?
You actually use function pointers to access ANY function in a library (like OpenGL is). All the functions listed in the LIB file are loaded to function pointers automatically. Anything that''s in the DLL by isn''t in the LIB you have to load manually, hence function pointers. Part of the problem is that Microsoft won''t update (and doesn''t allow anyone else to update) the LIB files with all of the new standard functions, so you end up having to load them by hand.
[Resist Windows XP''s Invasive Production Activation Technology!]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement