The void*
cast tries to convert the value of ptrtofun
, to a value of type pointer-to-void. C++ provides no such conversion from pointer-to-member-function to void*
, which is why there's something wrong with it (probably it doesn't compile).
The void*&
cast creates a reference, whose referand is the first sizeof(void*)
bytes of memory starting at the first byte occupied by the object ptrtofn
. ptrtofun
doesn't have type void*
or a compatible type, and need not even be the same size as void*
. So using the reference has undefined behavior (it's a violation of strict aliasing) but the compiler need not catch the problem.
Just about the only way to portably print a pointer-to-member function is to print sizeof(ptrtofun)
bytes starting from (unsigned char*)(&ptrtofun)
. You can decide for yourself exactly how you want to represent those bytes (hex being the obvious choice), but be aware that the result is not necessarily the address in memory of the code for TestClass::MyFunc
. Pointers-to-member-function simply don't work like that, because for example they need to allow for a pointer to a virtual function, which will call different code according to the dynamic type of the object that it's called on.