Skip to main content
gamedev.net
🔒 Locked 🎮 Unity

[C++] Simple Callback

Started by Headkaze Sep 22, 2010 at 1:50 PM 5 replies 1.7k views
Original Post
Headkaze
Headkaze
There seems to be alot of different solutions for implementing a callback in C++ from templates to std::tr1. I think the latter is the standard these days.

I have been searching around for a simple example that will work for me. Coming from a C# background it's a little daunting trying to find a simple solution.

I have a Transition class that I want to be able to set a callback in another class that will be called when the transition has changed state (Eg. Finished). It needs one parameter (an integer)

class Transition {public:	Transition();	~Transition();		void SetCallback(...) { ... }	void Update();	private:	... *m_callback;};


During the Update() call in certain circumstances I need to call the callback.

void Transition::Update(){	...	if(m_callback != NULL)		m_callback(0);}
Zipster
Zipster
For callbacks (delegates in C#), you have std::tr1::function, boost::function, fast delegates, and impossibly fast delegates, to name a few. I'm sure there are plenty of other implementations but these are the most popular and widely recommended.

For events, you have boost::signal or very fast events (can't think of any others off the top of my head but I'm sure they exist). The boost library has a ton of features but is rather heavyweight, but Sneftel's library is faster, more lightweight, and covers the most common event usage scenarios if you don't need all the bells and whistles.
ddlox
ddlox
class callback{public:  virtual void kikukuta1() = 0;  virtual int kikukuta2(int param) = 0;};class your_transition_class{  callback& my_cb;public:  your_transition_class(callback& somecallback):  my_cb(somecallback)  {  }   ...  void Update()  {    if (my_cb)      my_cb.kikukuta2(0);  }};class callback_implementation: public callback{  void kikukuta1()   {    printf("in kikukuta1\n");  }  int kikukuta2(int param)  {    printf("in kikukuta2 param is %d\n", param);  }};int main(){  callback_implementation ci;  your_transition_class ytc(ci);  ... stuff ...  ytc.Update();  ... stuff ...}  
Headkaze
Headkaze
Thanks for the info guys. I tried to implement ddlox's solution as it seemed simple

enum TransitionEvent{    TRANSITIONEVENT_CHANGEMODE,    TRANSITIONEVENT_FINISHED};class TransitionCallback{public:    virtual void OnTransitionEvent(TransitionEvent transitionEvent) = 0;};class Transition : public TransitionCallback{public:    Transition();    ~Transition();    void OnTransitionEvent(TransitionEvent transitionEvent) { };        void SetCallback(TransitionCallback& callback) { m_callback = callback; }    private:        TransitionCallback& m_callback;};...Transition::Transition() :	m_callback(OnTransitionEvent){}void Transition::Update(float elapsedTime){    m_callback.OnTransitionEvent(TRANSITIONEVENT_CHANGEMODE);}


I get the following errors

In constructor 'Transition::Transition()':error: invalid initialization of reference of type 'TransitionCallback&' from expression of type '<unresolved overloaded function type>'


I got an "error: could not convert '((Transition*)this)->Transition::m_callback' to 'bool':" when I had "if(m_callback)" so I thought to have a default "OnTransitionEvent" inside the Transition class.
CadetUmfer
CadetUmfer
m_callback is a reference, not a pointer. It cannot be null, and must be set in the constructor. To use it like you are trying to, make it a pointer. Unless it won't change for the lifetime of the instance, in which case keep it a reference and set it in the constructor (as per ddox's post)
Anthony Umfer
Headkaze
Headkaze
Thanks alot changing to pointers worked great.

Topic Locked

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

Sign in to reply to this topic.