C++ Is it possible to have a generic function pointer?

前端 未结 2 2137
北海茫月
北海茫月 2021-02-20 11:27

In C++ is it possible to make some sort of generic function pointer that points to any function that returns a pointer to some type and takes no arguments?

Eg, one type

2条回答
  •  我在风中等你
    2021-02-20 12:22

    Is there any way to cast the funcPointerA and B to C and D?

    Yes, you can explicitly cast one type of function pointer to another type:

    void* (*funcPointerC)() = reinterpret_cast(funcInt);
    

    But it is undefined behaviour to call the result of the cast, you must cast it back to its original type first. If you can record the original type and arrange for the pointer to be cast back to that original type then your code can work.

提交回复
热议问题