Skip to main content
2 of 5
added 26 characters in body
concept3d
  • 12.7k
  • 4
  • 45
  • 57

To solve your function signatures problems, people usually use function adapters whether they were function objects (e.g. boost::bind or you can write your own function object adapter if boost::bind seems like an overkill) or static functions adapters. To elaborate:

Using boost::bind:

  • using boost::bind is convenient you can bind almost any function, you can even use member functions as callbacks.

  • regarding performance boost::bind uses alot of small heap allocations which might not be a good idea in a performance critical applications (e.g. as Liosan mentioned Collision Detection). But anyway you should always profile and measure before judging.

  • Will introduce dependency on boost (might not be a problem for you).

  • Is Compatible with boost::function; which makes it even easier to move function objects around. On the other hand boost::function will.

  • boost::bind is not compatible (directly) with function pointers, so you won't be able to pass functions using bind as function pointers. both Win32 API and SDL use pure function pointers.

  • boost::bind "might" be inlined by the compiler, though boost::function will prevent inlining.

  • boost::bind can be

You can use static functions as Adaptors

  • Need to write alot of static functions.

  • No heap allocation.

  • You can still "bind" member functions using the "unsigned int arg" by sending the pointer "this" of your object example:

    class C { public: void EventHandler(void) { //Your Logic
    }

     static void OnHelper(unsigned long context) // you can send this as callback, and internally call your member function.
     {
       C* thisInstance  = reinterpret_cast<C*>(context); // Crash if used the wrong type
       C->EventHandler();
     }
    

    };

Writing your own function object adapter

  • You can customize it for your specific needs (example make it directly compatible with function pointers, be careful that the standard directly states that function pointers should not be stored in data pointers).

  • You may only need 2-3 parameters avoiding all the complexity boost::bind provides.

  • Will learn a couple of things (in case you have time).

  • Can be hard and/or time consuming.

  • No boost dependency.

as a side note Using STL Solution

  • STL introduced std::mem_fun, std::bind1st, std::2nd which can be useful in your case.

  • C++11 introduced lambda expressions (also part of boost) but I don't have experience with either.

concept3d
  • 12.7k
  • 4
  • 45
  • 57