This is the mail archive of the gsl-discuss@sources.redhat.com mailing list for the GSL project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

C++ wrapping


Hello all!

        I have (for various reasons) made my own C++
wrapper to several of the GSL routines (root-finding,
minimization, least-squares fitting, solution of
differential equations, random-number generation,
integration, differentiation, solution of
polynomials), and would like to hear everyone's
opinion about the best way implement these routines in
a object-oriented way. Here are my ideas. Tell me what
you think.

        First, I wouldn't want the user who wants to
solve some equation defined by a member function
double class::func(double x) to have to inherit from a
solver class. The reason is as follows: what if the
same class has another member function double
class:func2(double y) which the user wants to
integrate. Then the user has to inherit from both a
solver class and an integrator class. That sucks
because then multiple inheritance
comes into play. One could separate the functions
class::func() and class::func2() from the original
class, but if they have to communicate with each other
then things can get ugly.
        In order to make a generic solver class, then,
one needs some way to specify the function to solve. A
function pointer would work, i.e.   
solver::solve(double (*func)(double x)), but then the
member function has to be static. That also sucks,
because static functions cannot be virtual.
        So now I'm left to some sort of template
solution. I am presently a variant of the following
approach (a functor, I guess):

class function {
  double operator()(double x);
};
  
template <class T> class function_memb_fptr : public
function {
  function_memb_fptr(T *tp, double (T::*fp)(double)) {
    tptr=tp;
    fptr=fp;
  }
  double operator()(double x) { return
(*tptr.*fptr)(x); };
}
 
class solver {
        solve(double x, function f);
};
        
        So is there a better way? I'd love to hear any
ideas.
        
Thanks,
Andrew






__________________________________
Do you Yahoo!?
Yahoo! Small Business $15K Web Design Giveaway 
http://promotions.yahoo.com/design_giveaway/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]