Callback function , what is it

General discussion about C/C++

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

Callback function , what is it

Postby sapman » Thu Dec 02, 2010 11:10 am

In the name of God.

I know the function pointers .

Please Describe the Callback functions and using them in C++ .

Please Give me an Example ( Don't use Sorting functions , they are confusing me..);

Thanks in advance.
sapman
 
Posts: 8
Joined: Mon Oct 26, 2009 5:44 am

Re: Callback function , what is it

Postby ventsyv » Thu Dec 02, 2010 1:00 pm

Call back function is exactly what the name suggests a function that you "call back" after you've done your work. Think of it as passing a function pointer that you have to execute after the initial function has finished executing. Example:

Code: Select all

//Call
myFunction(status, HandleResultFunction);

//
myFunction(int, SomeFuncPointer)
{
   //Do work.

   //work is done send the result to a function that will handle it
   SomeFuncPointer
}


It's used in multi-threading application, it doesn't make much sense in single thread apps. MyFunction might contain a loop that runs every 5 seconds, so it never returns to the caller, but it's "sending" the results it generated to another function that process them (and is running in another thread).
User avatar
ventsyv
 
Posts: 2810
Joined: Mon Sep 22, 2003 5:25 pm
Location: MD USA


Return to General

Who is online

Users browsing this forum: No registered users and 1 guest