Hi!
First I am new to Fortran.
I have a situation of C++ class calling fortran function
which needs to call back a member function of that class.
Below is the code that takes an "ugly" approach. Please let
me if there are other better solutions. If not, is it
okay to do something like what I have done. The code compiles and runs fine on Solaris and Linux.
// First the "C" code:
#include <iostream>
class A
{
public:
A(){}
void print () { std::cout << "HelloWorld\n"; }
};
extern "C"
{
void dumpit_( unsigned long* aval )
{
A* a= reinterpret_cast<A*>(*aval);
a -> print();
}
}
extern "C"
{
void iamfortran_( unsigned long* );
}
int main()
{
A a;
unsigned long a_addr = reinterpret_cast<unsigned long>(&a);
iamfortran_( &a_addr );
}
! Next the F95 code:
subroutine iamfortran( l )
integer(8), intent(in) :: l
interface
subroutine dumpit( i )
integer(8), intent(in) :: i
end subroutine dumpit
end interface
call dumpit( l )
end subroutine iamfortran
First I am new to Fortran.
I have a situation of C++ class calling fortran function
which needs to call back a member function of that class.
Below is the code that takes an "ugly" approach. Please let
me if there are other better solutions. If not, is it
okay to do something like what I have done. The code compiles and runs fine on Solaris and Linux.
// First the "C" code:
#include <iostream>
class A
{
public:
A(){}
void print () { std::cout << "HelloWorld\n"; }
};
extern "C"
{
void dumpit_( unsigned long* aval )
{
A* a= reinterpret_cast<A*>(*aval);
a -> print();
}
}
extern "C"
{
void iamfortran_( unsigned long* );
}
int main()
{
A a;
unsigned long a_addr = reinterpret_cast<unsigned long>(&a);
iamfortran_( &a_addr );
}
! Next the F95 code:
subroutine iamfortran( l )
integer(8), intent(in) :: l
interface
subroutine dumpit( i )
integer(8), intent(in) :: i
end subroutine dumpit
end interface
call dumpit( l )
end subroutine iamfortran