C++/C function pointers that return void*

前端 未结 6 1340
故里飘歌
故里飘歌 2020-11-28 14:57

I\'m trying to call a function that takes an argument, void(*)(void*, int, const char*), but I cannot figure out how to pass those arguments to the function.

6条回答
  •  悲哀的现实
    2020-11-28 15:21

    You can't pass ptr(20) to this function, because you can only pass the pointer to the function but not the pointer with the argument. You may read about functors and theu will help you with such problem. Or the other solution is to change the signature to

    int function(int, int, void(*)(void) );
    

    And write function

    void ptr_wrap(void) {ptr(20);}
    

    so you can call function(20, 20, ptr_wrap);. But functors can solve this problem in more elegant way.

提交回复
热议问题