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

Function pointers to class methods

Started by Nacho Jan 14, 2008 at 10:40 AM 4 replies 800+ views
Original Post
Nacho
Nacho
Hi! My current project uses a very simple custom-made GUI system where each GUI object holds a function pointer to a callback function that is called whenever an event occurs (eg. a button is hovered, a list box item is selected, etc). The problem I'm facing right now is that I'd like that callback function to be a method of my CGameState class or one of its descendants but, after reading a few articles (including the parashift C++ faq) I think that this cannot be done in C++ via function pointers. Do you know any other method to achieve this? Thanks in advance for your answers, --Nacho
dave
dave
What is the problem you are having?
Can you paste some code?
jpetrie
jpetrie
You can use member-function pointers just fine.

You'll likely need to construct a suitable abstraction, or use an existing one, like Boost.Function and Boost.Bind, that allows you to treat functions more as first-class objects.
Zahlman
Zahlman
Quote:
Original post by Ignacio Liverotti
The problem I'm facing right now is that I'd like that callback function to be a method of my CGameState class or one of its descendants but, after reading a few articles (including the parashift C++ faq) I think that this cannot be done in C++ via function pointers. Do you know any other method to achieve this?


If you came away with the impression that it can't be done, you were definitely reading the wrong section of the faq. :) The main conceptual problem is simply: set the callback function to be a method of which CGameState object? But you should really just use functors to avoid that. In the simplest case (but it may be a bad hack) you just overload operator() for CGameState directly. Otherwise you can make a simple structure that holds a CGameState&, and overloads operator() to call a function on the referred-to CGameState.
Nacho
Nacho
Quote:
Original post by Dave
What is the problem you are having?
Can you paste some code?


Yes, of course. First, some background: every GUI widget derives from a base class named CGUIObj. This class has a member variable m_CallbackFnc of type GUI_CallbackFnc, which is defined as:

typedef int (*GUI_CallbackFnc) (CGUIObj *lpGUIObj,eGUIEventT eGUIEvent,lpInputEventParmsT lpInputEventParms,
CGameStateManager *lpGameStateManager);

The idea is that whenever an input event takes place (button press, mouse click, etc), the GUI manager (which holds a pointer to every created GUI object) goes through its object's list until it finds the one that is affected by the input event and calls its callback function.

The current approach works quite well but it's a little bit uncomfortable to deal with because the callback functions cannot be class methods and, as a result of that, at the beginning of most callbacks I have to retrieve the current game state (via the lpGameStateManager parameter) and some of its internal parameters (via get* functions) so that the callback can do its job.

What I'd need is some kind of callback which could be used for every method of any game state provided that those methods have the same signature. Notice that all the game states derive from the CGameState class.

Let me know if you want more info,

--Nacho
Nacho
Nacho
Quote:
Original post by Zahlman[...]But you should really just use functors to avoid that.[...]


Yes, I've read about those but -and please correct me if I'm wrong- shouldn't I be forced to create a class that derives from Funct for every game state?

--Nacho

Edit: Nevermind, I've just found this useful article at http://www.codeproject.com/KB/cpp/delegates_std.aspx that explains how to use functors with templates.

[Edited by - Ignacio Liverotti on January 14, 2008 12:35:05 PM]

Topic Locked

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

Sign in to reply to this topic.