locked
What is a call back function RRS feed

  • Question

  • What is callback function in C++.......... can any one explain with simple example..

    Thanks

    Thursday, May 16, 2013 1:51 PM

Answers

  • A callback function is simply a function that you provide to some other code that will be used by that other function to call back into your code. 

    They are very common in the Windows API.  Here is one example for calling EnumDisplayMonitors.

    There are a number of reasons you might use a callback.  One is when you want to receive notification of some event that happens and don't want to just wait around for the event.

    • Proposed as answer by Elegentin Xie Monday, May 20, 2013 5:52 AM
    • Marked as answer by Elegentin Xie Tuesday, May 21, 2013 7:08 AM
    Thursday, May 16, 2013 1:58 PM
  • >What is callback function in C++.......... can any one explain with simple example..

    It a function that's called indirectly from another one.

    When you call a function that requires a "callback" you in effect pass
    a pointer to the function you want "called back", and the function you
    call uses the pointer you pass to call your supplied function.

    The old qsort() is an example of a function that requires a user
    supplied "callback" to do the comparison.

    The Windows API has several functions that require callbacks - such as
    EnumWindows().

    In modern C++ you can often use Lambda functions to hide some of the
    implementation details of callbacks - though I often wonder if their
    syntax is less readable than the callback function would be.

    Dave

    • Proposed as answer by Elegentin Xie Monday, May 20, 2013 5:52 AM
    • Marked as answer by Elegentin Xie Tuesday, May 21, 2013 7:08 AM
    Thursday, May 16, 2013 2:03 PM

All replies

  • A callback function is simply a function that you provide to some other code that will be used by that other function to call back into your code. 

    They are very common in the Windows API.  Here is one example for calling EnumDisplayMonitors.

    There are a number of reasons you might use a callback.  One is when you want to receive notification of some event that happens and don't want to just wait around for the event.

    • Proposed as answer by Elegentin Xie Monday, May 20, 2013 5:52 AM
    • Marked as answer by Elegentin Xie Tuesday, May 21, 2013 7:08 AM
    Thursday, May 16, 2013 1:58 PM
  • >What is callback function in C++.......... can any one explain with simple example..

    It a function that's called indirectly from another one.

    When you call a function that requires a "callback" you in effect pass
    a pointer to the function you want "called back", and the function you
    call uses the pointer you pass to call your supplied function.

    The old qsort() is an example of a function that requires a user
    supplied "callback" to do the comparison.

    The Windows API has several functions that require callbacks - such as
    EnumWindows().

    In modern C++ you can often use Lambda functions to hide some of the
    implementation details of callbacks - though I often wonder if their
    syntax is less readable than the callback function would be.

    Dave

    • Proposed as answer by Elegentin Xie Monday, May 20, 2013 5:52 AM
    • Marked as answer by Elegentin Xie Tuesday, May 21, 2013 7:08 AM
    Thursday, May 16, 2013 2:03 PM
  • David, I find std::function the REAL winner in C++11 for cleaning up dirty syntax.  Lambdas are convenient but ugly, but being able to hide the hideous ugliness of C's function pointer syntax and being able to accept both function pointers and functors without turning your function into a template is the huge win.
    Thursday, May 16, 2013 2:57 PM
  • >David, I find std::function the REAL winner in C++11 for cleaning up dirty syntax.

    Got a link to a good example?

    I'm all for making code clean and readable providing it remains
    efficient, is understandable, and maintainable. I do have a bit of an
    aversion for things with triangle brackets though - I just can't read
    them easily!

    Dave

    Thursday, May 16, 2013 3:32 PM
  • David, I was mostly speaking on the other half of the equation (the half that receives the callback).

    I find code like:

    RegisterEvent(std::function<void(int)> eventCallback);

    to be much cleaner than:

    // limited to global/static functions only. No member functions void RegisterEvent(void (*eventCallback)(int)); // Can take member functions, but now this is a function template and

    //possibly forces a bunch of stuff into header files. Also tends to lead to

    //worse error messages, and can be difficult to store. template <typename T> void RegisterEvent(T eventCallback);

    Of course all this is a bit orthogonal to lamba functions themselves which may or may not be used as the callback parameters to any of the versions.


    • Edited by SimonRev Friday, May 17, 2013 3:38 AM
    Friday, May 17, 2013 3:36 AM
  • What is callback function in C++.......... can any one explain with simple example..


    Thanks


    As its name says, it is called by another function. For example in EnumWindows(), invokes EnumWindowsProc() itself until all top level windows are enumerated. In general we can compare it with recursive function except recursive invokes same function and callback invokes different function

    Thanks, Renjith V R

    Saturday, May 18, 2013 1:34 PM