help-gsl
[Top][All Lists]
Advanced

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

Re: [Help-gsl] Integration with C++ part2


From: Jordi Gutierrez Hermoso
Subject: Re: [Help-gsl] Integration with C++ part2
Date: Wed, 29 Nov 2006 14:47:16 -0600

On 29/11/06, Ivan Liu <address@hidden> wrote:
The following test routine works. But I'd like to use the member functions
of class Testo in the integrand function:
static double f (double x, void * params)
without declaring another object of class Testo ( pT in the example).

How can I do that?
[original code snipped out]

The problem seems to arise that you're declaring Testo::f to be
static, which in turn is necessary in order to be able to take its
address.

I think it makes more sense to declare f to be a non-member function,
and put it in some enclosing namespace.

I also don't understand what the point of the GetX function is
supposed to be. I removed it.

I think it makes sense to have Testo contain as a private member a
pointer to the function you actually want to integrate, and to
manipulate this pointer via member functions (which includes
constructors).

By the way, some stylistic points about your C++ code: it's not
necessary to use the "struct" qualifier when you're declaring objects
of type f_params (I think that's necessary for C, but it's certainly
not necessary in C++). Also, putting a global using declaration is
generally bad form. Put your using declarations inside other
namespaces or scoped inside functions or classes, or avoid them
altogether if you're only using a few objects from a specific
namespace. I have also added a destructor to your Testo class,
necessary since you allocated memory on the heap with new and with
malloc indirectly through GSL.

I attach my opinions of what your code should look like. My
modifications avoid the declaration of a new object as you requested,
and your class's destructors and constructors automatically take care
of memory management, while providing, IMHO, a more comfortable
syntax. These are the advantages of wrapping C code in C++.

HTH,
- Jordi G. H.

Attachment: test.cpp
Description: Text Data


reply via email to

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