Ask THE C ++ Pro 10-Minute Solution

zhaozj2021-02-17  53

Ask THE C Pro 10-Minute Solution

Declaring function Pointers and Implementing Callbacks, IMPLEMENTING CALLBACks

By Danny Kalev Programmers often need to implement callbacks. I will discuss the fundamentals of function pointers and show how to use them to implement callbacks. Notice that this article focuses on ordinary functions, not class member functions, which rely on substantially different syntactic and semantic rules (pointers to class members were discussed in a previous 10-Minute Solution) Declaring a function PointerA callback function is one that is not invoked explicitly by the programmer;. rather the responsibility for its invocation is delegated to another function that receives the callback function's address. to implement a callback, you need to define an appropriate function pointer first. Although the syntax is a bit arcane, if you're familiar with function declarations in general, you will notice that a function pointer declaration is very similar to a function Declaration. consider the folowing example: void f (); // a function prototype

IT Declares A Function F () That Takes No Arguments and Returns Void. A Pointer To Such A Function Has The Following Type: Void (*);

Let's parse it. The asterisk in the leftmost parentheses is the nucleus of a function pointer declaration. Two additional elements are the function's return type, which appears on the left and is 'void' in our example, and a parameter list enclosed in the rightmost parentheses. In our case, the parameter list is empty because f () takes no arguments. Note that we did not create a pointer variable yet-we only declared the type of such a variable. We can use this type to create a typedef Name, or in a sizeof expression: // Get the size of a function Pointerunsigned psize = sizeof (void (*));

// Declare a typedef for a function Pointer

Typedef void (* pfv) ();

pfv is a synonym for "a pointer to a function that takes no arguments and returns void". We can use this typedef name to hide the cumbersome syntax of function pointers. A pointer variable, of course, has a name. Here is an example Of Such a Pointer: Void (* p) (); //p is a pointer to a function

p is a pointer to a function that takes no arguments and returns void. The name of a pointer variable appears on the right of the asterisk, inside the parentheses. We can now assign a value to p. A value is simply a name of a Function That Has A Matching Signature (Parameter List) And Return Type. for example: void func ()

{

/ * Do Something * /

}

P = func;

You can assign a different value to p as long as it's the address of a function with the same signature and return type. A function's name is not a part of its type, though.Passing an Address of a Callback Function to Its CallerNow we can Pass P To Another Function, Caller (), Which Will Call The Function To Which P P P P P P P P P P P P P PPINTS (THE CALLEE) WITHOUT KNOWING ITS NAME: Void Caller (Void (* Ptr) ()) {

Ptr (); / * Call the function to which ptr points * /

}

Void func ();

int main ()

{

P = func;

Caller (P); / * Pass Address of Func to Caller * /

}

If you assign a different function to p, caller () will invoke that function. The assignment can take place at runtime, which enables you to implement dynamic binding. Calling ConventionsUp until now, we've discussed function pointers and callbacks without discussing compiler- specific conventions that are not defined by ANSI C / C . Many compilers have several calling conventions. For example, in Visual C you can precede __cdecl, __stdcall or __pascal to a function's type to indicate its calling convention (__cdecl is the default). C Builder also supports the __fastcall calling convention. The calling convention affects the compiler-generated name of a given function (ie, name mangling), the order in which arguments are passed (right to left or left to right), stack cleanup responsibility ( By The Caller or the Callee, And The Mechanism for Argument Passing (stack, cpu registers, etc .. it's important to note what the calling convention is an in turn; you Can't Assign An Address of a Function TO A Pointer with An Incompatible Calling Convention. for example: // callee is a function Takes Int and returns int__stdcall int callee (int);

// Caller is a function this takes a function Pointer

Void Caller (__CDECL INT (* PTR));

// Illegal Attempt to Store The Address of Callee in P

__cdecl int (* p) (int) = callee; // error

p and callee () have incompatible types because they have different calling conventions. Therefore, you can not assign callee's address to the pointer p, although both have the same return value and parameter list.

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

New Post(0)