Giuliano Guest
|
Posted: Tue Jul 18, 2006 3:55 am Post subject: pointers to member functions |
|
|
Hi to all in the newsgroup. I have a class:
class CrossSection {
public:
// ctor , dtor etc.
double getValue(double E);
double integrate(double a, double b); // <-- i'd like to
implement *this
private:
// private data used by getValue
}
what i need now is to calculate the integral with respect to energy of
the cross section.
cross section values are obtained by a call to getValue(E). So, i'd
like to implement a member
function integrate(), whose arguments are the two integration limits a
and b, and whose return value is the "area" under the cross section
curve. Very simple, isn't it? I've at my disposal a few *global*
integration routines (they differ in the basic numerical algorithm),
that, like all numerical integration routines, accept various
parameters the most important of which is a function pointer (the
routines
deference it internally to obtain the function values at various
abscissae). That is:
typedef double (*f_ptr)(double )
int integrate_gauss (f_ptr, double a, double b, double *result, double
*erel)
int integrate_gauss_legendre (f_ptr, double a, double b, double
*result, double *erel)
int integrate_adapt_step (f_ptr, double a, double b, double *result,
double *erel, size_t maxiter)
etc..
in my bad programmer's ingenuity (!) i've written something like:
double integrate(double a, double b) {
double erel = 1e-8;
double result;
integrate_gauss(&getValue, a,b,&result,&erel);
return result;
}
that obviously doesn't work (it fails to compile) because the set of
functions *integrate_xyz*
expect a pointer to a "normal" i.e. global function, not to a member
function...i've felt a bit sad after a couple or so of screens full of
compilator errors.
The fact is that I definitely NEED a member function getValue, because
it uses the object's specific internal data (constructore reads them
from a file). I've a lot of such CrossSection objects, stored in an
array.
Static, "extern C " member function could be used as arguments to
integrate_xyz functions but i don't know ho to access specific object
data without any modification to the mathematical library (obviously I
must pass the the address of the specific object instance i.e. should
be something like: double globalGetValue(CrossSection &cross, double
E)).
What to do? I think this should be a more or less common problem, but i
can't find by myself a solution that does't imply an heavy
modification, debugging, adjustment ... cycle of all integration
routines (and of all ancillary subroutines, obviously!). I feel that
must exist a much more simple solution to this problem but my actual
knowledge (or habit to) of the language is probably not sufficient to
make me find this solution ... ideas? In any case thanks a lot!
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|