开发者

What's the easiest way to print the address of a member function?

If I have a base class with a pure-virtual function, and a derived class that implements that function - what's the easiest way I can print out the address that the function actually gets called at?

class A { public: virtual void func()=0; }

class B:A { 
    public: 
        void func() { /*implementation*/ }开发者_运维知识库
        void func2() { *** I WANT TO PRINT THE ADDRESS OF func() HERE! *** }
};

Also, what's the easiest way to print out the address of a static function in a class, and to print out the address of a global function?


c++ does not give any portable means of printing the address of a virtual function.


The easiest way is to figure out the option to get your linker to output a link map, and look up the function name there.


[Edited] As far as I know, there's no way to get the address of the function that actually will be called, regardless of virtualness. For virtual methods specifically, the method and destination of virtual dispatch is left to the implementation. However even for normal functions the function pointer may not be the actual code address of the function (although I suspect there are cases where it is).


A method pointer is not always an address. It is in fact (on gcc) two things:

  • An offset
  • An address or an index

When the last bit of the member function pointer is 1 the second part is the offset in the vtable which indicate the method to call otherwise it is the address of the member function. The offset is added to the this pointer.

That's why a member function pointer is bigger than a common pointer. Also, you should notice that contrary to the function pointer, there is an overhead using member function pointer.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜