在GTK库中,可以找到以下定义:
/**
* GCallback:
*
* The type used for callback functions in structure definitions and function
* signatures. This doesn't mean that all callback functions must take no
* parameters and return void. The required signature of a callback function
* is determined by the context in which is used (e.g. the signal to which it
* is connected). Use G_CALLBACK() to cast the callback function to a #GCallback.
*/
typedef void (*GCallback) (void);
这大概是函数指针的typedef,它不带任何参数并返回void
。
但是,在GTK网站的Hello World示例中,显示了以下代码:
g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
activate
是返回void
但有两个参数的函数。 (G_CALLBACK
是仅转换为GCallback
的宏。)>
的确是-GCallback
typedef上方的注释表明:
这并不意味着所有回调函数都不能使用任何参数然后返回void
此代码确实可以编译并运行。那怎么可能呢?
在GTK库中,可以找到以下定义:/ ** * GCallback:* *用于结构定义和函数*签名中的回调函数的类型。这并不意味着...
C11标准§6.3 Conversions,更准确地说§6.3.2.3 Pointers ¶8说: