WittyGuy wrote:
Quote:
How to typecast a "function pointer" to "const void*" type in C++ way?
There is no "how".
Quote:
int MyFunction (double money); // Function prototype
>
const void* arg = (const void*)MyFunction; // type casting function
pointer to const void* in C-style
>
void(*pFunc)() = (void(*)())(arg); // type casting const void* to function
pointer in C-style
(*pFunc)(); // Calling the function after type casting is done
You are casting back to a different signature. Even if there was a roundtrip
guarantee through void* (which there is not), this conversion would be
unspecified.
Quote:
Is typecasting like above is safe anyway?
No: In C++ there is no round-trip guarantee for pointer-to-function or
pointer-to-member-function to void* and back. Just don't do it.
Function pointers are convertible to a different signature, however, the
result of such conversion cannot be used: it can only be converted back.
However, this does allow you to use void(*)(void) as a universal function
pointer to and from which you can cast (reinterpret_cast is the one you
might want to use) [5.2.10/6].
Best
Kai-Uwe Bux