How to print member function address in C++

前端 未结 4 1570
星月不相逢
星月不相逢 2020-12-01 09:40

It looks like std::cout can\'t print member function\'s address, for example:

#include 

using std::cout;
using std::endl;

clas         


        
4条回答
  •  攒了一身酷
    2020-12-01 10:21

    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!

提交回复
热议问题