Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Calling Fortran subroutine with abstract class

steinberg__elad1
Beginner
592 Views

Hello,

I wish to call a Fortran subroutine from c++. However, one of the arguments in the subroutine is an abstract class. Attached is a simple example of an abstract class, a derived class and the calling subroutine. Is it possible to call such a subroutine from c++? How would I declare and pass the derived class?

 

module Fabstract
    implicit none

    type, abstract :: arithmetic
    contains
        procedure(a_action), deferred :: action
    end type arithmetic

    abstract interface

    subroutine a_action(this, number, result)
        import :: arithmetic
        class(arithmetic) :: this
        real(8), intent(in) :: number
        real(8), intent(out) :: result
    end subroutine a_action
 
    end interface

    type, extends(arithmetic) :: multiply
        real(8) :: number_value 
    contains   
        procedure :: action => multi_action
    end type multiply
  
    contains  
  
     subroutine multi_action(this, number, result)
        class(multiply) :: this
        real(8), intent(in) :: number
        real(8), intent(out) :: result
    
        result = this%number_value * number
     end subroutine multi_action
    
    
end module Fabstract

 

 

module Fabstract_use
use Fabstract
implicit none

public :: math_op
contains

subroutine math_op(op, number)
    class(arithmetic), intent(in) :: op
    real(8), intent(in) :: number
    real(8) :: caluclated_value

    call op%action(number, caluclated_value)
    write(*, *) caluclated_value
end subroutine math_op

end module Fabstract_use

 

0 Kudos
2 Replies
Steve_Lionel
Honored Contributor III
592 Views

The answer is simple - no, you cannot do this. There is no C (nor C++)  interoperability for Fortran polymorphic entities

0 Kudos
steinberg__elad1
Beginner
592 Views

Thank you

0 Kudos
Reply