Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
Welcome to the Intel Community. If you get an answer you like, please mark it as an Accepted Solution to help others. Thank you!
26754 Discussions

Calling Fortran subroutine with abstract class

steinberg__elad1
Beginner
118 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
Black Belt Retired Employee
118 Views

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

steinberg__elad1
Beginner
118 Views

Thank you

Reply