- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hello All,
I have been searching to find information on passing classes as arguments from C++ to Fortran to C++, but with no luck. I acutally thought this would be a common problem, but maybe everyone that has figured it out is hoarding their answers?
The issue at hand is that I would like to use a private member friend function of a class to solve an ODE. So, I am attempting to create a class with a friend function (FCN) that I can send to an ODE solver in Fortran which then calls FCN (back in C++) to perform a part of the numerics. The FCN function will change based on which class is sent to the solver, otherwise I might just hard code it in Fortran and be done with it.
Thanks!
Jason C.
I have been searching to find information on passing classes as arguments from C++ to Fortran to C++, but with no luck. I acutally thought this would be a common problem, but maybe everyone that has figured it out is hoarding their answers?
The issue at hand is that I would like to use a private member friend function of a class to solve an ODE. So, I am attempting to create a class with a friend function (FCN) that I can send to an ODE solver in Fortran which then calls FCN (back in C++) to perform a part of the numerics. The FCN function will change based on which class is sent to the solver, otherwise I might just hard code it in Fortran and be done with it.
Thanks!
Jason C.
Link Copied
3 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Quoting - jdchambless
Hello All,
I have been searching to find information on passing classes as arguments from C++ to Fortran to C++, but with no luck. I acutally thought this would be a common problem, but maybe everyone that has figured it out is hoarding their answers?
The issue at hand is that I would like to use a of a class to solve an ODE. So, I am attempting to create a class with a friend function (FCN) that I can send to an ODE solver in Fortran which then calls FCN (back in C++) to perform a part of the numerics. The FCN function will change based on which class is sent to the solver, otherwise I might just hard code it in Fortran and be done with it.
Thanks!
Jason C.
I have been searching to find information on passing classes as arguments from C++ to Fortran to C++, but with no luck. I acutally thought this would be a common problem, but maybe everyone that has figured it out is hoarding their answers?
The issue at hand is that I would like to use a of a class to solve an ODE. So, I am attempting to create a class with a friend function (FCN) that I can send to an ODE solver in Fortran which then calls FCN (back in C++) to perform a part of the numerics. The FCN function will change based on which class is sent to the solver, otherwise I might just hard code it in Fortran and be done with it.
Thanks!
Jason C.
Note that "member friend function" is a self contradiction - you are either a member or a friend.
Attached is an example that I think does what you want. It uses a free function on the C++ to act as the interface between Fortran and C++. That free function unwraps an opaque pointer (void*) to an object (the object having been previously created on the C++ side and passed over to Fortran), and then a virtual function call is made on the C++ side to access the desired function. This virtual function is a public member (making it clear that it is going to be called from outside the class), but obviously once you are "inside" the class you can call any private member that you want to.
All Fortran knows about the C++ side is that it must call a certain C function and pass that function the C_PTR argument that it was given earlier. It knows nothing about the internals of the C++ classes, nor could it. If it munges the C_PTR argument somehow all hell will break loose.
You can do very similar things as the C++ side is doing here (runtime polymorphism) using the object oriented programming features of F2003.
IanH
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I have not yet gotten to try the code, but thank you for the response!
Also, from what you are saying, it seems to be impossible to access class member variables from inside Fortran; is that correct?
Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
You can, sort of, as I understand it.
Fortran can interoperate with C-structs, subject to some limitations. C-structs are (generally/always?) compatible with C++ classes that are "pod" (plain old data) types. So fortran can interoperate with C++ classes that are of pod type.
But there are many restrictions on pod types, including no private data members, no virtual functions, no user defined constructors, no base classes, no saturated fats, etc, etc, etc (list very incomplete...).
A pod type could be a member of a normal C++ class, and you could pass that pod "class" object (declared as a struct more typically) back and forth with Fortran. Fortran could then merrily fiddle with the members of that pod type. But it can't then directly call member functions of that pod class - function calls have to be through a C style interface.
IanH
Fortran can interoperate with C-structs, subject to some limitations. C-structs are (generally/always?) compatible with C++ classes that are "pod" (plain old data) types. So fortran can interoperate with C++ classes that are of pod type.
But there are many restrictions on pod types, including no private data members, no virtual functions, no user defined constructors, no base classes, no saturated fats, etc, etc, etc (list very incomplete...).
A pod type could be a member of a normal C++ class, and you could pass that pod "class" object (declared as a struct more typically) back and forth with Fortran. Fortran could then merrily fiddle with the members of that pod type. But it can't then directly call member functions of that pod class - function calls have to be through a C style interface.
IanH

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page