How to get this pointer from std::function?

我与影子孤独终老i 提交于 2019-12-05 00:02:39

问题


Since std::function can hold member functions, so it must store a pointer to the object instance somewhere.

How can I fetch the this pointer from a std::function that holds a member function?


回答1:


An object of type std::function holds a callable object. A pointer to member function is a kind of callable object; it can be called with an argument of the appropriate class type, plus any additional arguments that it needs. For example:

struct S {
    void f(int);
};
std::function<void(S, int)> g(&S::f);

To call it, pass an object of type S:

S s;
g(s, 3);

Note that the std::function object does not hold an S object; it's only when you call it that the function pointer gets bound to an object.



来源:https://stackoverflow.com/questions/15747669/how-to-get-this-pointer-from-stdfunction

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