1

I would like to call a FORTRAN function from my C++ code. The FORTRAN function is something like this

extern "C" { void FORTRANFUNC( void FCN(int*,double*), int *N); }

The FCN function reported above is a member function of a C++ class

class CppClass 
{
...
void FCN(int* a, double* b);
...
};

I would like to call the code in this way, but it seems that it is not possible:

FORTRANFUNC(myClass.FCN, &n_);

The compiler complains because the FORTRAN functions wants a void FCN function, while I am passing a CppClass::void function.

Can you help me?

alberto.cuoci
  • 87
  • 2
  • 5

1 Answers1

1

Member function pointers are not pointers. They require special handling on the call-site. Since Fortran code is unlikely to know the drill, what you're trying to do is impossible. You must pass a free function instead. Since there is no void* argument for arbitrary user data, the only workaround (and I stress that: this is fugly workaround) you have is to use a global for dispatch (well, or create a thunk dynamically, but that's no so easy):

CppClass *obj;
void dispatch_fcn(int* a, double* b) {
    obj->fcn(a, b);
}

// later:
obj = &myClass;
FCN(dispatch_fcn, &n_);
Cat Plus Plus
  • 125,936
  • 27
  • 200
  • 224