How to print member function address in C++

前端 未结 4 1575
星月不相逢
星月不相逢 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:22

    I'd like to add to the other answers, that the reason that you are getting '1' printed instead of an address, is that, for some reason, the compiler is coercing your function pointer into a boolean, so that you are really calling ostream& operator<< (bool val);

    This seems to be unrelated to the function being a member function.

    You can uncover this kind of information with clang++ -cc1 -ast-dump:

    (ImplicitCastExpr 0x3861dc0  '_Bool' 
        (UnaryOperator 0x3861940  'void (class TestClass::*)(void)' prefix '&'
            (DeclRefExpr 0x38618d0  'void (void)' CXXMethod 0x3861500 'MyFunc' 'void (void)')))))
    

提交回复
热议问题