help-gsl
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-gsl] GSL, C++, and function pointers


From: Paulo Jabardo
Subject: Re: [Help-gsl] GSL, C++, and function pointers
Date: Tue, 6 Mar 2007 18:49:50 -0300 (ART)

> Void pointers scare me. They're too much like
> unprotected C. :-)
> 
> > The params field is a pointer to user data. It can
> be
> > anything you want so you can define a
> structure/class
> > that contains every information necessary. Then
> you
> > write a global function (as defined by GSL) that
> knows
> > how to use this structure/class you defined.
> 
> I don't understand how this is supposed to work, so
> I'll avoid it. My
> wrapper class will take care of the param fields by
> having them as
> data members.
> 

That's the "problem" with C. Since there is no
polymorphism/etc the only way to pass generic
parameters to a function is using a void*, at least
that's the way I see it and that is why gsl_function
has this void parameter. Here is what I would do:

class MyClass
{
  double x,y,z;
  std::vector<double> a,b,c;
  // other paramters your program uses...
public:
  double call(double v); //Or whatever
  double operator()(double v){return call(v);}
};

// Now the wrapper to gsl_function that calls MyClass:

extern "C"{
double myclass_call(double x, void *params)
{
   MyClass *this = (MyClass *) params;
   return this->call(x);
}
}

// To use this, you simply...
int main()
{
  gsl_function fun;
  MyClass simul;
  double result, abserr;
  double x, h;
  //... Do whatever you need to setup simul
  
  fun.function = &myclass_call;
  fun.params = (void *) (&simul);

  gsl_deriv_central(&fun, x, h, &result, &abserr);
}

You shouldn't be afraid of the void*. It is the
mechanism that GSL and several other libraries to
provide some flexibility. I hope this example helps.

Paulo Jabardo

__________________________________________________
Fale com seus amigos  de gra├ža com o novo Yahoo! Messenger 
http://br.messenger.yahoo.com/ 




reply via email to

[Prev in Thread] Current Thread [Next in Thread]