是否有可能从模板中存储类而不使整个类成为模板?模板和函数指针
任务:
我有两个功能,V1无参数和参数V2, 如果V1被称为地方什么也没有发生与使用(),如果V2被称为地方使用()应执行function_ptr与我从DoSometh(T *)获得的实例。
例如
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'对象,普通(或静态成员)函数指针和lambda表达式,而不用担心实例或存储信息。 –
'void *'或者甚至更好 - 'ParentInterface *' – Amartel
显示一个真实的用例示例。看起来好像可能有更好的解决方案。 –