views:

255

answers:

6

Hi,

I realise this is a newbie question, but as I'm trying to learn C++ I'm stumpling upon this expression "callback" frequently. I've googled it and checked wikipedia, but without finding a good explination. I am familiar with some Java and C#, but how unlikely it sounds, I have never really understood what a callback means.

If someone know how to explain this term to a simple layman, I would be really thankfull.

+3  A: 

This has your answer and code references: Callback

RC
+2  A: 

A callback is a hook into the code that is executing to allow you to provide customised features at known points in the process. It allows for generalised control structures to perform customised operations which are specified by your code which is called from within them, hence the term "call back" - it calls back into your code.

Generally this is done by you providing a function pointer with a specific pre-defined signature and the generic code doing the call back will pass parameters to your function and expect a return value of a certain type.

It is a really powerful pattern which allows for code re-use and quite simple customisation/extension without completely re-writing.

Custom sort functions are a great example. The sort algorithm is generic, the comparison function is specific to what is being sorted. Many algorithms allow you to provide a function which takes two arguments of generic types, being the objects to compare, and expects you to return a +ve, -ve or zero value depending on the result of the compare.

You then write the comaprison function yourself and provide a function pointer to the sort algorithm which it "calls back" during sorting.

Simon
+3  A: 

When you send something a callback, you send it a way of addressing a function (for example, a function pointer in C++) so that the code you're sending it to can call that function later, when it's completed some process.

The difference between

start_some_process(some_value, some_function())   # not using a callback

and

start_some_process(some_value, some_function)     # using a callback

is that in the first instance you're sending the result of the function, while in the second instance you're sending the function itself.

chaos
+1  A: 

In the simplest terms a callback is a code that you pass into another method.

E.g. you have a class A which calls a method on class B but you need some code running from Class A when it's finished. You put your code in its own new method on class A and you pass the method name in when you call the method on class B. When the method on class B has done its stuff it can 'call back' into class A.

Nowadays, you don't actually need to put the callback code in its own method: you've got anonymous methods and lambda expressions you can use. I think it's probably least confusing (at least, in C#) to learn using anonymous methods until you get it.

Good luck!

P.S. I was the same: I was coding C# for years before I really understood them.

serialhobbyist
+9  A: 

I am familiar with some Java and C#

A callback is an event or delegate in those languages - a way to get your code run by somebody else's code in it's context. Hence, the term "callback":

  1. You call some other piece of code
  2. It runs, perhaps calculating an intermediate value
  3. It calls back into your code, perhaps giving you that intermediate value
  4. It continues running, eventually passing control back to you by completing

A canonical example is a sort routine with a user defined comparison function (the callback). Given a sort routine such as:

void Sort(void* values, int length, int valueSize, 
          int (*compare)(const void*, const void*) 
{
   for (int i = 0; i < length; i = i + 2) {
      // call the callback to determine order
      int isHigher = compare(values[i], values[i + 1]);
      /* Sort */
   }
}

(The specifics of how the sort is performed isn't important - just focus on the fact that any sorting algorithm needs to compare 2 values and determine which is higher.)

So, now we can define some comparison functions:

int CompareInts(const void* o, const void* p) {
   int* a = (int*) o;
   int* b = (int*) p;

   if (a == b) return 0;
   return (a < b) ? -1 : 1;
}

int ComparePersons(const void* o, const void* p) {
   Person* a = (Person*) o;
   Person* b = (Person*) p;

   if (a == b) return 0;
   return (a->Value() < b=>Value()) ? -1 : 1;
}

And reuse the same sort function with them:

int intValues[10];
Person personValues[10];

Sort(intValues, 10, sizeof(intVaues[0]), CompareInts);
Sort(personValues, 10, sizeof(personVaues[0]), ComparePersons);

Things get a bit more complicated if you're using member functions, as you have to manage the this pointer - but the concept is the same. As with most things, it's easier to explain them in C first. ;)

Mark Brackett
>A callback is an event or delegate in those languagesThe Observer pattern is also a construct that emulates callback-like behavior.
StackedCrooked
A: 

in C++ check out boost::function

http://www.boost.org/doc/libs/1_39_0/doc/html/function.html

George