Let's see how to implement the callback in C + +.
Method1: use global function as callback
Callbacks in the C language are convenient. Of course, we can use a C-way callback function in C + +, that is, to define a global function as a callback function and then call it.
typedef void (*PCALLEDFUN) (int *);
void Getcallback (Pcalledfun parafun)
{
/*do something*/
}
If we want to use the Getcallback function, we need to implement a pcalledfun type of callback function:
void Funcallback (int *inum)
{
* Do something * *
}
The funcallback can then be passed directly to Getcallback as a variable,
Getcallback (Funcallback);
There may be several invocation specifications for the compiler. For example, in Visual C + +, you can add _cdecl,_stdcall to a function type to represent its calling specification (default is _CDECL). The calling specification affects the given function name produced by the compiler, the order in which the parameters are passed (from right to left, or left to right), the Stack cleanup responsibility (caller or callee), and the parameter passing mechanism (stack, CPU register, etc.). Take a look at the following example:
#include <iostream> using namespace std; typedef void (__stdcall *pfun) (void); typedef void (__cdecl *pfunc) (void); void __stdcall Textprint (void) {cout << ' call back like Pascal ' << Endl} void __cdecl Textprintc (void) {Co UT << "Call back like C" << Endl; } void Fortext (Pfun pFun1, Pfunc pFun2) {pFun1 (); pFun2 ();} void Main (void) {//pfun PP = textprint;//pfunc PPC = textp RINTC; PP (); PPC (); Fortext (Textprint, TEXTPRINTC); }
method 2 : use a static function of a class as a callback
Since the use of C + +, you can not always live in the shadow of C, we want to use the class, Class, Class ...
Let's use the static function of the class as a callback, why the static function first, because the static function is similar to the global function, the function call does not use the this pointer, we can use the same as the global function of the static function. As follows:
#include <iostream> using namespace std; typedef void (*pfun) (void); Class Ccallback {public:static void textprint (void) {cout << "static Callback Function of a class" << Endl; } }; void Fortext (Pfun pFun1) {pFun1 ();} void Main (void) {Fortext (ccallback::textprint);}
Of course, we can encapsulate the TypeDef into classes and increase cohesion.
#include <iostream> using namespace std; Class Ccallback {public:typedef void (*pfun) (void), static void Textprint (void) {cout << "static Callback Functio N ' a Class with Funtype ' << Endl; } }; void Fortext (ccallback::p fun pFun1) {pFun1 ();} void Main (void) {Fortext (ccallback::textprint);}
method 3 : using an imitation function as a callback
The above two methods used to feel or in the way of C, since it is C + +, to object-oriented, to have objects. So let's take a look at the copy function. The so-called imitation function, is to make the use of a class looks like a function, the essence is in the class overloaded operator operator (), this class has a similar function of the behavior, is an imitation function class. The advantage is that you can design, maintain, and manage your code with object-oriented considerations. Many do not say, see examples:
#include <iostream> using namespace std; typedef void (*fun) (void); inline void Textfun (void) {cout << ' Callback Function ' << Endl;} class Textfunor {public:void operator () (v) OID) const {cout << "Callback functionor" << Endl;}; void Fortext (Fun pfun, Textfunor cfun) {pfun (); Cfun ();} void Main (void) {Textfunor cfunor; Fortext (Textfun, Cfunor); }
Cite a little introduction to imitation functions:
The advantages of imitation functions (functor)
My suggestion is that if you can use an imitation function, you should use an imitation function instead of a callback.
The reason is:
An imitation function can pass context parameters without trace. The callback technique is usually passed with an additional void* parameter. This is also why most people think callback technology is ugly.
Better performance. Imitation function technology can get better performance, this is more difficult to understand intuitively. You might say that the callback function is inline, and how the performance is worse than the imitation function. Let's analyze it here. We assume that a function func (for example, the std::sort above) passes a callback function (such as the Compare above), which can be divided into two situations:
Func is an inline function, and relatively simple, the Func call is eventually expanded, and the call to the callback function becomes a normal function call (rather than an indirect call through the function pointer), and if the callback function is simple, it may also be expanded at the same time. In this case, the callback function is the same as the performance of the affine function.
Func is not an inline function, or rather complex to expand (for example, the above std::sort, we know it is a quick sort, the function is unable to expand because of recursion). The callback function is passed as a function pointer and its code cannot be expanded. and The imitation function is different. Although the func itself is complex and cannot be expanded, the invocation of an Func function in the functions of a compiler can be determined and inline expanded during compilation. So in this case, the affine function has better performance than the callback function. Also, this performance advantage is sometimes an unparalleled advantage (as for std::sort, because the number of elements compared is very large and can be inline