C++ typedef 函数指针,并在一个语句中声明一个指针



我有一个 c++ 程序,它导入了一个包含许多函数的 dll。

//.h file
/*Description: set velocity(short id of axis, double velocity value)*/
typedef short(__stdcall *GT_SetVel)(short profile, double vel);
//.cpp file
/*Description: set velocity(short id of axis, double velocity value)*/
GT_SetVel SetAxisVel = NULL;
...
SetAxisVel = (GT_SetVel)GetProcAddress(GTDLL, "_GT_SetVel@10");
...
SetAxisVel(idAxis, vel);

我想让它更紧凑,比如

//.h file
/*Description: set velocity(short id of axis, double velocity value)*/
typedef short(__stdcall *GT_SetVel)(short profile, double vel) SetAxisVel = NULL;
//.cpp file
SetAxisVel = (GT_SetVel)GetProcAddress(GTDLL, "_GT_SetVel@10");
...
SetAxisVel(idAxis, vel);

这听起来可能很荒谬。是否有类似于上述的语法,其中两个语句合并为一个,而不仅仅是放在一起放入 ajacent 行中。

原因是
(1(我需要类型别名和函数指针变量,
(2(并且有必要为typedef(语义通过参数列表进行参数描述(和指针声明(提供智能感知供以后使用(都有描述注释。

但是类型别名只使用一次,在两个单独的位置插入相同的描述似乎是多余的。

有没有办法让它更紧凑?谢谢。

通过摆脱typedef,你可以缩短为:

// .cpp
/*Description: set velocity(short id of axis, double velocity value)*/
short(__stdcall *SetAxisVel)(short profile, double vel) = NULL;

SetAxisVel = reinterpret_cast<decltype(SetAxisVel)>(GetProcAddress(GTDLL, "_GT_SetVel@10"));

最新更新