【发布时间】:2010-09-16 14:50:03
【问题描述】:
我有一种方法可以为类调用延迟函数:
//in MyClass declaration:
typedef void (MyClass::*IntFunc) (int value);
void DelayedFunction (IntFunc func, int value, float time);
class TFunctorInt
{
public:
TFunctorInt (MyClass* o, IntFunc f, int v) : obj (o), func (f), value (v) {}
virtual void operator()();
protected:
MyClass* obj;
IntFunc func;
int value;
};
//in MyClass.cpp file:
void MyClass::DelayedFunction (IntFunc func, int value, float time)
{
TFunctorBase* functor = new TFunctorInt (this, func, value);
DelayedFunctions.push_back (TDelayedFunction (functor, time)); // will be called in future
}
void MyClass::TFunctorInt::operator()()
{
((*obj).*(func)) (value);
}
我想制作模板仿函数。第一个问题是:
template <typename T>
typedef void (MyClass::*TFunc<T>) (T param);
导致编译器错误:“'typedef' 的模板声明”。 有什么解决办法?
PS:代码基于http://www.coffeedev.net/c++-faq-lite/en/pointers-to-members.html#faq-33.5
【问题讨论】:
-
如果我错了,请纠正我(这种语法远非简单,我太懒了^Hbusy 无法检查自己),但不能将
((*obj).*(func)) (value)拼写为obj->*func(value)吗? -
@sbi:
(obj->*func)(value)准确地说。
标签: c++ templates pointers function