Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There isn't an explicit concept of a callback function in C++. Callback mechanisms are often implemented via function pointers, functor objects, or callback objects. The programmers have to explicitly design and implement callback functionality.</p> <p><em>Edit based on feedback:</em></p> <p>In spite of the negative feedback this answer has received, it is not wrong. I'll try to do a better job of explaining where I'm coming from.</p> <p>C and C++ have everything you need to implement callback functions. The most common and trivial way to implement a callback function is to pass a function pointer as a function argument.</p> <p>However, callback functions and function pointers are not synonymous. A function pointer is a language mechanism, while a callback function is a semantic concept. Function pointers are not the only way to implement a callback function - you can also use functors and even garden variety virtual functions. What makes a function call a callback is not the mechanism used to identify and call the function, but the context and semantics of the call. Saying something is a callback function implies a greater than normal separation between the calling function and the specific function being called, a looser conceptual coupling between the caller and the callee, with the caller having explicit control over what gets called. It is that fuzzy notion of looser conceptual coupling and caller-driven function selection that makes something a callback function, not the use of a function pointer.</p> <p>For example, the .NET documentation for <a href="http://msdn.microsoft.com/en-us/library/system.iformatprovider.aspx" rel="noreferrer">IFormatProvider</a> says that <em>"GetFormat is a callback method"</em>, even though it is just a run-of-the-mill interface method. I don't think anyone would argue that all virtual method calls are callback functions. What makes GetFormat a callback method is not the mechanics of how it is passed or invoked, but the semantics of the caller picking which object's GetFormat method will be called.</p> <p>Some languages include features with explicit callback semantics, typically related to events and event handling. For example, C# has the <em>event</em> type with syntax and semantics explicitly designed around the concept of callbacks. Visual Basic has its <em>Handles</em> clause, which explicitly declares a method to be a callback function while abstracting away the concept of delegates or function pointers. In these cases, the semantic concept of a callback is integrated into the language itself.</p> <p>C and C++, on the other hand, does not embed the <em>semantic concept</em> of callback functions nearly as explicitly. The mechanisms are there, the integrated semantics are not. You can implement callback functions just fine, but to get something more sophisticated which includes explicit callback semantics you have to build it on top of what C++ provides, such as what Qt did with their <a href="http://qt-project.org/doc/qt-4.8/signalsandslots.html" rel="noreferrer">Signals and Slots</a>.</p> <p>In a nutshell, C++ has what you need to implement callbacks, often quite easily and trivially using function pointers. What it does not have is keywords and features whose semantics are specific to callbacks, such as <em>raise</em>, <em>emit</em>, <em>Handles</em>, <em>event +=</em>, etc. If you're coming from a language with those types of elements, the native callback support in C++ will feel neutered.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload