【发布时间】:2016-09-30 19:08:39
【问题描述】:
我正在尝试使用模板将一个类方法传递给另一个类方法,但找不到任何关于如何做的答案(没有 C++11,boost ok):
我将核心问题简化为:
class Numerical_Integrator : public Generic Integrator{
template <class T>
void integrate(void (T::*f)() ){
// f(); //already without calling f() i get error
}
}
class Behavior{
void toto(){};
void evolution(){
Numerical_Integrator my_integrator;
my_integrator->integrate(this->toto};
}
我得到错误:
error: no matching function for call to ‘Numerical_Integrator::integrate(<unresolved overloaded function type>)’this->toto);
note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘void (Behavior::*)()’
谢谢。
奖励:参数呢?
class Numerical_Integrator{
template <class T, class Args>
double integrate(void (T::*f)(), double a, Args arg){
f(a, arg);
}
}
class Behavior{
double toto(double a, Foo foo){ return something to do};
void evolution(){
Foo foo;
Numerical_Integrator my_integrator;
my_integrator->integrate(this->toto, 5, foo};
}
【问题讨论】:
-
Boost function 和 Boost bind?或者看看standard algorithm library 看看他们如何处理“谓词”?
-
你需要 T 做什么?验证?
-
f(a, arg);this 的对象。 -
@thorsan:我希望能够为集成()提供任何东西。 @ coyotte508:好的,但是编译器即使不调用 f() 也会报错,请参见第一个示例中的注释行。
标签: c++ templates c++03 member-functions