How to print member function address in C++

I don’t believe that there are any facilities provided by the language for doing this. There are overloads for operator << for streams to print out normal void* pointers, but member function pointers are not convertible to void*s. This is all implementation-specific, but typically member function pointers are implemented as a pair of values – a flag indicating whether or not the member function is virtual, and some extra data. If the function is a non-virtual function, that extra information is typically the actual member function’s address. If the function is a virtual function, that extra information probably contains data about how to index into the virtual function table to find the function to call given the receiver object.

In general, I think this means that it’s impossible to print out the addresses of member functions without invoking undefined behavior. You’d probably have to use some compiler-specific trick to achieve this effect.

Hope this helps!

Leave a Comment