Function pointers casting in C++

前端 未结 8 1416
星月不相逢
星月不相逢 2020-11-27 15:45

I have a void pointer returned by dlsym(), I want to call the function pointed by the void pointer. So I do a type conversion by casting:

void *gptr = dlsym(         


        
8条回答
  •  无人及你
    2020-11-27 16:16

    This may help you. It prints "Hello".

    #include 
    
    void hello()
    {
      std::cout << "Hello" << std::endl;
    }
    
    int main() {
      typedef void (*fptr)();
      fptr gptr = (fptr) (void *) &hello;
      gptr();
    }
    

    OR you can do:

    fptr gptr = reinterpret_cast( (void *) &hello);
    

    where &hello is replaced by the dlsym command.

提交回复
热议问题