get function member address

风格不统一 提交于 2019-12-02 02:40:14

问题


Is there any way to get the exact address of a function member? For example I have :

struct foo
{
    void print() { printf("bla bla bla"); }
};
//////////////////////////////////////////////////////////////
unsigned int address = foo::print;

回答1:


You can use the following syntax to declare the pointer to the member function:

typedef void (foo::*address)();
address func = &foo::print;

In order to call non-static member function you will need an existing instance of that class:

(fooInstance.*func)();



回答2:


I'm not sure what you mean by "exact address". There's certainly no way of putting any address in an unsigned int (which is smaller than a pointer on my machine). For that matter, there's no way of putting a pointer to a function in a void*. Again, I've used machines where pointers to a function were larger than void*. And finally, there's no way of putting a pointer to (non-static) member function into a pointer to function; pointer to member functions are almost always larger. Finally, given:

void (MyClass::*pmf)();
MyClass* p;
(p->*pmf)();

mais call different functions, depending on the contents of p.

So it's not at all clear what you're asking for.



来源:https://stackoverflow.com/questions/14578435/get-function-member-address

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!