Callback

Three ways to to pass a callback function:


void CallbackSomething(int nNumber, function callback_function) 
{
    // Call the specified 'function'
    callback_function(nNumber);
}

// Function
void IsEven(int n)
{
   std::cout << ((n%2 == 0) ? "Yes" : "No");
}

// Class with operator () overloaded
class Callback
{
public:
   void operator()(int n)
   {
      if(n<10)
         std::cout << "Less than 10";
      else
         std::cout << "More than 10";
   }
};

int main()
{
   // Passing function pointer
   CallbackSomething(10, IsEven);

   // Passing function-object
   CallbackSomething(23, Callback());

   // Another way..
   Callback obj;
   CallbackSomething(44, obj);

   // Locally defined lambda!
   CallbackSomething(59, [](int n)    { std::cout << "Half: " << n/2;}     );
}

some useful tips (mostly for myself)