【发布时间】:2017-02-23 18:07:58
【问题描述】:
我正在刷新一些关于函数指针的被遗忘的概念,以创建某种包装类来处理回调调用。
在谷歌上冲浪后,我发现了这篇简单但有趣的帖子 http://blog.coldflake.com/posts/C++-delegates-on-steroids/
到目前为止,除了这个,我几乎什么都懂
T::*TMethod
我记得 ::* 的意思,你可以在帖子的很多地方看到,这里是作者第一次使用它
class Delegate
{
typedef void (*Type)(void* callee, int);
public:
Delegate(void* callee, Type function)
: fpCallee(callee)
, fpCallbackFunction(function) {}
template <class T, void (T::*TMethod)(int)> <<<<<<First time
static Delegate from_function(T* callee)
{
Delegate d(callee, &methodCaller<T, TMethod>);
return d;
}
void operator()(int x) const
{
return (*fpCallbackFunction)(fpCallee, x);
}
private:
void* fpCallee;
Type fpCallbackFunction;
template <class T, void (T::*TMethod)(int)>
static void methodCaller(void* callee, int x)
{
T* p = static_cast<T*>(callee);
return (p->*TMethod)(x);
}
};
到目前为止,我了解它的工作原理,但我不记得 ::* 的含义
我知道这可能是一个非常基本和愚蠢的问题,所以请客气。
最好的问候
【问题讨论】:
-
它是一个成员函数指针
标签: c++