分享
 
 
 

Ask the C++ Pro 10-Minute Solution

王朝c/c++·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

Ask the C++ Pro 10-Minute Solution

Declaring Function Pointers and 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 Pointer

A 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 following 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 didn't 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 pointer

unsigned 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 Caller

Now we can pass p to another function, caller(), which will call the function to which p points (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 Conventions

Up until now, we've discussed function pointers and callbacks without discussing compiler-specific conventions that aren't 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 (i.e., 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 that the calling convention is an integral part of a function's type; you can't assign an address of a function to a pointer with an incompatible calling convention. For example:

// callee is a function that takes int and returns int

__stdcall int callee(int);

// caller is a function that takes a function pointer

void caller( __cdecl int(*ptr)(int));

// 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't assign callee's address to the pointer p, although both have the same return value and parameter list.

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有