class TwoArgFcn { public: 虚拟双重评估(双x,双t)= 0; }; 然后派生类根据需要定义evaluate()并且你的解算器 采用类型为TwoArgFcn&的对象。或者类似的东西。 如果你使用模板,那么你的解算器就会模仿 函数对象的类型: 模板< typename FcnClass> 类解决方案 { public: Solver(FcnClass& ; input_fcn):input_fcn(input_fcn){} private: void solve(){/ *使用input_fcn.f()* /} FcnClass& input_fcn; }; 这里你假设模板类FcnClass定义了一些函数f (将在以后验证模板已经过实例化。 -Mark I am a c++ newbie, so please excuse the ignorance of this question.I am interested in a way of having a class call a general memberfunction of another class.Specifically, I am trying to write an ordinary differential equationclass that would solve a general equation in the form:dx/dt = f(x,t).The ode class shouldn''t know anything about f, except how to call it.My first thought was to have a data member that stores a pointer tothe function f like:double (*f) (double, double);However, I learned that f could not point to a non-static memberfunction of a different class. This is bad for me, because I need touse the solver for functions that are complicated to evaluate anddepend on many parameters. A simple example:class my_f{ public:double a;double f(double x, double t){ return t+x*a;}};my_f foo; foo.a = 7;I would like the ode solver to call foo.f(x,t). I could makedouble (my_f::* f)(double, double);a member of the ode solver, but then I would have to write a new odesolver for every new class of functions to be used for f or to haveall these derive from some kind of base class, which I would prefer toavoid.My question is: is there a simple and elegant way to do this? I wouldthink that similar issues have been encountered many times before.Thanks. 解决方案This can help give you an idea how to approach this problem.-----------------------------------------------// Use boost/function and functors#include <boost/function.hpp>// create a functorclass my_f{public:my_f( double a ) : m_a(a) {}double operator()(double x, double t){ return t+x*m_a;}private:double m_a;};// this is another functorclass my_y{public:my_y( double a ) : m_a(a) {}double operator()(double x, double t) { return t+x*m_a;}private:double m_a;};class ODE{public:void solve( boost::function<double (double, double)f){// call f heredouble result = f( 1.0, 1.0 );}};intmain(){ODE solver;solver.solve( my_f(20.0) );solver.solve( my_y(100.0) );}I believe you''re making a conceptual mistake here. In thedesign you''re describing f(x,t) is not represented by amethod of a class, but by the class itself, and f a (x,t) -by an instance of the class representing f(x,t) functions.That''s why you probably shouldn''t try passing a pointer toa method, pass a reference to the object instead. If youhave more general questions about all of this, you probablyshould follow-up to comp.object.That''s pretty much a textbook description of an interface.[...]That kinda defeats the purpose of OOD, doesn''t it?I''m not sure if this will work for you, but consider thisbare-bones implementation:#include <iostream>class interface_function{public :virtual double operator ( )( double x , double t ) const = 0 ;} ;class ode{public :static void solve ( const interface_function & f ) ;} ;void ode :: solve ( const interface_function & f ){std :: cout <<f(static_cast < double ( 1 ) ,static_cast < double ( 1 )) ;std :: cout << std :: endl ;}class f : public interface_function{public :double a_ ;f ( ) : a_ ( 1 ) { }virtual double operator ( )( double x , double t ) const ;} ;double f :: operator ( )( double x , double t ) const{return t + x * a_ ;}class g : public interface_function{public :double a_ ;g ( ) : a_ ( 1 ) { }virtual double operator ( )( double x , double t ) const ;} ;double g :: operator ( )( double x , double t ) const{return t - x * a_ ;}int main ( ){f f1 ;g g1 ;ode :: solve ( f1 ) ;ode :: solve ( g1 ) ;f1 . a_ = 5 ;g1 . a_ = 5 ;ode :: solve ( f1 ) ;ode :: solve ( g1 ) ;}--roy axenovHow are you passing the function object to the solver in the firstplace? Or perhaps this is really the root of your question? The twomost direct approaches would be to use either inheritance or templates.In the former, you would have some abstract base class like:class TwoArgFcn{public:virtual double evaluate( double x, double t) = 0;};Then a derived class defines evaluate() as appropriate and your solvertakes an object of type TwoArgFcn& or something similar.If you use templates, then you solver is templated on the type of thefunction object:template <typename FcnClass>class Solver{public:Solver( FcnClass& input_fcn) : input_fcn( input_fcn) {}private:void solve() { /* use input_fcn.f() */ }FcnClass& input_fcn;};Here you assume that the template class FcnClass defines some function f(which will be verified when the template is instatiated).-Mark 这篇关于将成员函数指针传递给函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-18 04:01
查看更多