On 06.01.2017 12:37, kushal bhattacharya wrote:
> Is callbackback function used in current application projects(in
> C++)?
Yes, just like loops are used, and so on. Callback functions are a basic
technique. A main difference between C and C++ is that in C++ a callback
function might be a template argument or associated with a template
argument, and then the calls of it can be inlined, like in `std::sort`
(which is therefore generally faster than C's `qsort`).
> If so then how do we define a callback function.
Depends on the code that's calling it.
> My scenario is, i want to set the callback function in a function
> first and then when a particular event is being notified i want to
> get that callback function called.What is the best way to do that
Depends on your definition of “best”, which is nowhere in sight.
Some possible choices for callback:
* Ordinary free function, possibly with a state argument.
* For a member function + object, the result of `std::mem_fn`.
* A `std::function` object.
* An object of a specified class with a virtual method (the callback).
* Some custom delegate (C# terminology) kind instead of `std::mem_fn`
result; before C++11 there was a plethora of 3rd party such classes.
I think that list is exhaustive or very nearly so.
Callbacks for events are a bit tricky because you run into `const`
correctness issues: differentiating between `const` and non-`const`
objects for event callbacks is usually, IME, not a practical idea.
Cheers!,
- Alf