Software Archive
Read-only legacy content
17061 Discussions

Calling C++ Class methods from Fortran

jmdawk
Beginner
416 Views
I'm just trying to clarify whether or not it is possible to call a member function of a C++ Object from Fortran.

I'm assuming it's not possible but the way to get around it, would be to call a regular static C function which would then be used as a link between the desired Object's method and Fortran.

Thanking whoever can clarify this in advance,
Dawkins
0 Kudos
1 Reply
Jugoslav_Dujic
Valued Contributor II
416 Views
As you probably know, C++ member functions have this as the first hidden parameter; thus, in theory, you could call them from Fortran provided you get a C pointer to the class object somehow, write a proper INTERFACE block and call the method, passing the pointer as the first argument.

Another issue could be calling conventions; AFAIK MSVC++ uses __fastcall as the default, where two arguments are passed through registers and the rest using stack. I don't know the details (and I'm not even sure about the above statement), but that cannot be emulated in VF. Thus, I think you'd be able to do it only if you declare the member functions __stdcall or __cdecl in VC++. (If it's another compiler, check the documentation about default calling convention). Note also that MSVC mangles names in a naughty way by default (e.g. ?_Foo@YAXXUZ), so your INTERFACE block must take that into account too.

Jugoslav
0 Kudos
Reply