代码之家  ›  专栏  ›  技术社区  ›  ricecakebear

C++在一个语句中定义函数指针并声明指针

  •  0
  • ricecakebear  · 技术社区  · 9 年前

    //.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) 我需要类型别名和函数指针变量,

    有没有办法使它更紧凑?谢谢

    1 回复  |  直到 9 年前
        1
  •  1
  •   Jarod42    9 年前

    通过去掉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"));