模板和函数指针
Template and function pointer
是否可以通过某种方式从模板中存储 class 而无需将整个 class 设为模板?
任务:
我有两个函数,v1 没有参数,v2 有参数,
如果 v1 在某处被调用 Use() 没有任何反应,如果 v2 在某处被调用 Use() 应该使用我从 DoSometh(T*).
获得的实例执行 function_ptr
例如
class MyClass
{
//v1 no parameters
void DoSomething()
{
}
//v2 with parameter
template<class T>
void DoSomething(T* instance, void (T::*func)())
{
store somewhere?? = instance;
}
void Use()
{
//if DoSometh(T* instance) was used before
if(instance != NULL)
{
(*instance->)//call function pointer from DoSomething(T*,void (T::*)())
}
}
}
std::function problem
update:
class Timer : public ITickable
{
std::function<void()> test; //adding this does weird things
virtual void Tick() {}
}
class MyClass
{
ITickable* tickable_;
void Tick()
{
tickable_->Tick(); //let's assume it points to a Timer obj.
}
}
我认为 std::function
和 std::bind
(C++11) 确实完成了您想要的,正如评论中已经建议的那样。计时器 class 的简化模型可能是:
class Timer
{
std::function<void()> m_task;
public:
template <typename T>
void setTask(T &instance, void (T::*fcn)()) // consider T const & if applicable
{
m_task = std::bind(fcn, &instance);
}
void fire()
{
if (m_task) // std::function overloads operator bool()
m_task();
}
};
当 setTask
被一个对象和一个可以在这个对象上调用的成员函数调用时,一个 std::function
对象被创建(你当然可以选择在构造函数中这样做).当计时器触发时,将检查此对象(使用 operator bool()
,由 std::function
提供),如果它是可调用的(例如,当之前调用过 setTask()
时),它会调用该函数。
例如:
class MyClass
{
public:
void func()
{
std::cout << "Hi from MyClass\n";
}
};
class MyOtherClass
{
public:
void func()
{
std::cout << "Hi from MyOtherClass\n";
}
};
int main(int argc, char **argv)
{
MyClass x1;
MyOtherClass x2;
Timer t1, t2;
t1.setTask(x1, &MyClass::func);
t2.setTask(x2, &MyOtherClass::func);
t1.fire();
t2.fire();
}
是否可以通过某种方式从模板中存储 class 而无需将整个 class 设为模板?
任务:
我有两个函数,v1 没有参数,v2 有参数, 如果 v1 在某处被调用 Use() 没有任何反应,如果 v2 在某处被调用 Use() 应该使用我从 DoSometh(T*).
获得的实例执行 function_ptr例如
class MyClass
{
//v1 no parameters
void DoSomething()
{
}
//v2 with parameter
template<class T>
void DoSomething(T* instance, void (T::*func)())
{
store somewhere?? = instance;
}
void Use()
{
//if DoSometh(T* instance) was used before
if(instance != NULL)
{
(*instance->)//call function pointer from DoSomething(T*,void (T::*)())
}
}
}
std::function problem
update:
class Timer : public ITickable
{
std::function<void()> test; //adding this does weird things
virtual void Tick() {}
}
class MyClass
{
ITickable* tickable_;
void Tick()
{
tickable_->Tick(); //let's assume it points to a Timer obj.
}
}
我认为 std::function
和 std::bind
(C++11) 确实完成了您想要的,正如评论中已经建议的那样。计时器 class 的简化模型可能是:
class Timer
{
std::function<void()> m_task;
public:
template <typename T>
void setTask(T &instance, void (T::*fcn)()) // consider T const & if applicable
{
m_task = std::bind(fcn, &instance);
}
void fire()
{
if (m_task) // std::function overloads operator bool()
m_task();
}
};
当 setTask
被一个对象和一个可以在这个对象上调用的成员函数调用时,一个 std::function
对象被创建(你当然可以选择在构造函数中这样做).当计时器触发时,将检查此对象(使用 operator bool()
,由 std::function
提供),如果它是可调用的(例如,当之前调用过 setTask()
时),它会调用该函数。
例如:
class MyClass
{
public:
void func()
{
std::cout << "Hi from MyClass\n";
}
};
class MyOtherClass
{
public:
void func()
{
std::cout << "Hi from MyOtherClass\n";
}
};
int main(int argc, char **argv)
{
MyClass x1;
MyOtherClass x2;
Timer t1, t2;
t1.setTask(x1, &MyClass::func);
t2.setTask(x2, &MyOtherClass::func);
t1.fire();
t2.fire();
}