Jump to content
  • Advertisement
Sign in to follow this  
athile

C++ callback methods

This topic is 4841 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

This is obviously a contrived example, but I'm wondering if there's a more concise way to do what I'm doing below in C++. Basically, I want to wrap a bunch of functions (not methods) in classes, so I can use them as callbacks. The pattern I have below works fine for this:

// Function I want to wrap
void draw_function(int a, int b, int c, int d, int e)
{
  // Do some useful stuff here...
}

// General callback interface 
struct Callback {
  virtual void draw() = 0;
}

// Wrapper for draw_function
struct DrawFunctionCallback
{
  DrawFunctionCallback(int a, int b, int c, int d, int e)
    : m_a(a), m_b(b), m_c(c), m_d(d), m_e(e)
  { }

  virtual void draw()
  {
     draw_function(m_a, m_b, m_c, m_d, m_e);
  }

  int m_a;
  int m_b;
  int m_c;
  int m_d;
  int m_e;
};



The annoynance I have is that I have to repeat the argument list 5 times just make simple function call wrapper (once in the constructor argument list, twice in the initializer list, once in the actual function call, and once as member variables. It's just an annoyance, but I was wondering if anyone had any suggestions on a cleaner, more concise way of handling this [outside of using an external script, coding in an alternate programming language, etc.] There might be no easy way around this...not sure. Thanks.

Share this post


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

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!