Declare function pointer and achieve callback

zhaozj2021-02-11  154

Declare function pointer and achieve callback

Programmers often need a callback. This article discusses the basic principles of function pointers and explains how to use function pointers to achieve callbacks. Note that it is a common function, which does not include class member functions that are fully dependent on different grammar and semantic rules (class member pointers will be discussed in another discussion).

The declaration function pointer callback function is a function that the programmer cannot explicitly call; by transmitting the address of the callback function to the caller to achieve calls. To achieve a callback, you must first define a function pointer. Although the defined syntax is a bit incredible, if you are familiar with the general method of the function declaration, it will find that the declaration of the function pointer is very similar to the function declaration. Please see the example below: void f (); // Function prototype The statement declares a function, no input parameters and returns Void. Then the function pointer declaration is as follows: void (*) (); let us analyze, the asterisk in the left circular proximity is the key to the function pointer declaration. The other two elements are the return type (VOID) of the function and the entrance parameters in the edge circlans (the parameters in this example are empty). Note The pointer variable has not been created in this example - just declares the variable type. Currently, this variable type can be used to create a type definition name and obtain the size of the function pointer with a sizeOf expression: // Get the size of the function pointer unsigned psize = sizeof (void (*)); / / Function pointer declare type definition TypeDef void (* pfv) (); PFV is a function pointer, which does not point to the input parameters, return class behavior VOID. Use this type definition name to hide complex function pointer syntax. The pointer variable should have a variable name: void (* p) (); // P is a pointer P in a function pointing to a function of a function, the function does not input parameters, the type of return value is Void. The appearance of the arc in the left is the pointer variable name. With a pointer variable, you can assign a value, the content of the value is the function name and return type of the signature. For example: void func () {/ * do something * /} p = func; P's assignment can be different, but must be the address of the function, and the signature and return type are the same. The address of the transfer function can now pass P to another function (caller) - Caller (), which will call the P pointing function, and this function name is unknown: Void Caller (Void (* Ptr) ()) {PTR (); / * Call the function of PTR * /} void func (); int main () {p = func; caller (p); / * Transfer function address to the caller * /} if Different values ​​to p (different function addresses), then the caller will call a function of different addresses. The assignment can occur when running, so that you can realize dynamic binding. Call specifications so far, we only discuss function pointers and callbacks without paying attention to the compiler specification of ANSI C / C . Many compilers have several invoking specifications. For example, in Visual C , you can add _cDecl, _stdcall, or _pascal in front of the function type, indicate its call specification (default to _cDecl). C Builder also supports _fastcall call specification. Call specification affects the given function name generated by the compiler (from right to left or from left to right), stack cleaning responsibility (caller or called by caller) and parameter transfer mechanism (stack, CPU register, etc.) . It is important to see that the call specification is part of the function type; the address cannot be assigned to the function pointer with an incompatible call specification.

转载请注明原文地址:https://www.9cbs.com/read-5734.html

New Post(0)