float __stdcall (*pFunc)(float a, float b) = (float (__stdcall *)(float,float))0x411280;
如何使用调用约定声明函数指针?以上给了我一个错误。
float __stdcall (*pFunc)(float a, float b) = (float (__stdcall *)(float,float))0x411280;
如何使用调用约定声明函数指针?以上给了我一个错误。
诀窍是将 __stdcall 放在括号内,如下所示:
float (__stdcall *pFunc)(float a, float b) = (float (__stdcall *)(float,float))0x411280;
当然,建议您改用 typedef,但同样的技巧也适用:
typedef float (__stdcall *FuncType)(float a, float b);
对于常规功能,您通常可以执行以下操作:
__cdecl const int func();
__cdecl const int (func)();
const __cdecl int func();
const int __cdecl func();
const int (__cdecl func)();
__cdecl const __cdecl int (__cdecl func)();
编译器是否接受所有这些形式是由实现定义的。铿锵声。对我来说,第 5 版最符合语义,因为它是函数的属性,而不仅仅是返回类型。
您可以使用__attribute__((cdecl))
代替来完成所有这些操作__cdecl
,它也可以在函数之后使用,不像__cdecl
const int (__cdecl func)() __attribute__((cdecl));
现在,要声明一个指向pfunc
具有特定调用约定的函数的常量指针:
__cdecl const int (*const pfunc)();
const __cdecl int (*const pfunc)();
const int __cdecl (*const pfunc)();
const int (__cdecl *const pfunc)();
const int (*const __cdecl pfunc)();
const int (*__cdecl const pfunc)();
__cdecl const __cdecl int (__cdecl *const pfunc)();
const int (*const pfunc)() __attribute__((cdecl));
请注意,const
必须像往常一样在星号之后。使用双函数指针,指针可以根据调用约定去任何地方,但您必须将它放在相对于const
.
同样,它是定义为编译器接受什么形式的实现。Clang 接受所有形式并将它们正确解释为类型const int (*const)() __attribute__((cdecl))
__fastcall
是优化的(最快的调用约定),但由于未知原因未使用
尝试:
int (__fastcall *myfunction)(int,float);