Jump to content
  • Advertisement
Sign in to follow this  
Nacho

Function pointers to class methods

This topic is 3987 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!