Mixed Fortran/C++ Help Needed

Born2bwire

Diamond Member
Oct 28, 2005
9,840
6
71
I need to use an adaptive numerical integration routine for my code. I have previously rewritten one of the routines from Quadpack in C++ to take handle double complex arguments. I am having some trouble with the integration for some of my cases and I want to try some of the other Quadpack routines. Instead of translating the code, I want to just modify the Fortran and call the Fortran instead.

Not a problem, except that the functions that need to be evaluated are written in C++ and that they require me to pass a pointer to a class object that holds needed data. So the interface for the function that needs to be evaluated in C++ is:

Dcmplx Integrand(FunctionValues* values, Dcmplx x);

I normally declare the pointer to be passed to the C++ integration routine as:

void IntergrationRoutine(Dcmplx(ClassName::*f)(FunctionValues*, Dcmplx), ...);

The extern that I would like to have for the Fortran is:

void dqag_(Dcmplx(ClassName::*f)(FunctionValues*, Dcmplx), Dcmplx *a, ..., FunctionValues* passvalues);

So I want to pass the pointer to the C++ function that needs to be evaluated and a pointer to the object that holds the necessary data. Is there a way to do this with Fortran? I know that Fortran cannot be used to manipulate or access C++ classes, but in this case I do not need it to. I just need Fortran to take in a memory address and pass it onto the C++ function when it makes the call. Is there a way to do this? And if so, what is the syntax for all of this (I am working off of the syntax that worked for calling Fortran from C++ but I have never done the reverse)? I guess I could "hack" it by declaring an integer of appropriate size, writing the pointer's memory address into the integer, and then passing it into Fortran and cast it as a pointer in the C++ integrand function, but I am compiling this for 32 bit and 64 bit systems so such a hack would require me to edit the code depending upon the size of my memory addresses.
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
I do not know what Fortran version you are using.

Best thing would be to look at the syntax for Fortran to handle the situation. Wiether look at the ANSI spec or you compilers Help section.

My Fortran experience has grown stale by 15 years so I can not provide much more assistance than that.