c/c++语言开发共享如何声明一个返回函数指针的函数?

想象一个带有参数double和int的函数myFunctionA:

myFunctionA (double, int); 

这个函数应该返回一个函数指针:

 char (*myPointer)(); 

如何在C中声明此function?

     void (*fun(double, int))(); 

    根据左右规则 , fundouble, int的函数double, int返回一个指向函数的指针,该函数返回void参数不确定。

    编辑: 这是该规则的另一个链接。

    编辑2:这个版本只是为了紧凑和显示它真的可以完成。

    在这里使用typedef确实很有用。 但不是指针,而是函数类型本身

    为什么? 因为它可以作为一种原型使用,所以确保function真正匹配。 并且因为作为指针的身份仍然可见。

    所以一个好的解决方案就是

     typedef char specialfunc(); specialfunc * myFunction( double, int ); specialfunc specfunc1; // this ensures that the next function remains untampered char specfunc1() { return 'A'; } specialfunc specfunc2; // this ensures that the next function remains untampered // here I obediently changed char to int -> compiler throws error thanks to the line above. int specfunc2() { return 'B'; } specialfunc * myFunction( double value, int threshold) { if (value > threshold) { return specfunc1; } else { return specfunc2; } } 

    typedef是你的朋友:

     typedef char (*func_ptr_type)(); func_ptr_type myFunction( double, int ); 

    制作一个typedef:

     typedef int (*intfunc)(void); int hello(void) { return 1; } intfunc hello_generator(void) { return hello; } int main(void) { intfunc h = hello_generator(); return h(); } 

     char * func() { return 's'; } typedef char(*myPointer)(); myPointer myFunctionA (double, int){ /*Implementation*/ return &func; } 

      以上就是c/c++开发分享如何声明一个返回函数指针的函数?相关内容,想了解更多C/C++开发(异常处理)及C/C++游戏开发关注计算机技术网(www.ctvol.com)!)。

      本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

      ctvol管理联系方式QQ:251552304

      本文章地址:https://www.ctvol.com/c-cdevelopment/544977.html

      (0)
      上一篇 2021年1月9日
      下一篇 2021年1月9日

      精彩推荐