Skip to main content
GameDev.net gamedev.net
🔒 Locked

c related

Started by basuprasen Aug 11, 2006 at 12:59 AM 1 replies 650+ views
Original Post
basuprasen
basuprasen
When would I use a pointer to a function
Fruny
Fruny
When you need to pass a function to another function. For example, qsort is written so that it can sort any data type, but you need to pass it a function that knows how to compare two elements.

Or when you want to change the function that gets called in some circumstances, e.g. when a button is pressed. Or if you want to re-implement virtual functions by hand.

Etc.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Zahlman
Zahlman
With modern C++ facilities, it's quite unusual to need it. When you do, you'll probably end up asking "how do I do this" and getting an answer about function pointers. Except that you'll actually probably more likely get an answer about functionoids (also called functors), because they're strictly more powerful.

Of course, if you really are stuck with C, you might need them a fair bit more (but again, not for everyday stuff). But then, why would you be stuck with C these days? :(

Topic Locked

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

Sign in to reply to this topic.