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

Pointers to Functions in C++

Started by Nervo Nov 24, 2003 at 2:32 PM 3 replies 700+ views
Original Post
Nervo
Nervo
I have the general idea of how to use them syntactically, but in particular I wanted to know how often do most of you use them? Additionally, what uses are there of them where they are the only straightforward solution to a given problem? It appears that it makes a called function that contains the pointer more versatile, but I really need to see what is the huge gain from it to open my mind up to it. Thanks.
Well, R2D22U2..
DukeAtreides076
DukeAtreides076
I think function pointers have mostly been replaced by virtual functions in C++. Virtual functions are actually implemented with function pointers. The only things I''ve ever seen where you had to use them was with runtime self-modifying code.
Sneftel
Sneftel
Their primary use is as "callback functions", where you use a function pointer to tell another module what to do when a particular event occurs.

In C++, function pointers are not nearly as useful as in C; they've largely been eclipsed by functors and by virtual classes.


"Sneftel is correct, if rather vulgar." --Flarelocke

[edited by - sneftel on November 24, 2003 3:55:21 PM]
jdhardy
jdhardy
I prefer to boost::bind and boost::function, which are nice wrappers around functors and function pointers, and allow you to treat functions as any other variable.

They're also in the draft C++0x spec, so you might as well learn them now.

Callbacks, as Sneftel said, are the primary usage - say as an action in a GUI system or any other event driven system.

[edited by - ze_jackal on November 24, 2003 4:58:59 PM]

Topic Locked

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

Sign in to reply to this topic.