C ++ typedef函数指针并在一个语句

时间:2017-08-14 07:24:46

标签: c++ function-pointers intellisense 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);

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

原因在于 (1)我需要类型别名和函数指针变量,
(2)并且必须对typedef(语义通过参数列表进行参数描述)和指针声明(提供intellisense以供以后使用)具有描述注释。

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

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

1 个答案:

答案 0 :(得分:1)

通过删除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"));