C++ typedef 函数指针并在一个语句中声明一个指针
C++ typedef function pointer and declare a pointer in one statement
我有一个 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);
这听起来可能很荒谬。有没有类似于上面的语法,其中两个语句合并为一个,而不是放在一起相邻的行。
原因是
(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"));
我有一个 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);
这听起来可能很荒谬。有没有类似于上面的语法,其中两个语句合并为一个,而不是放在一起相邻的行。
原因是
(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"));