1

我知道您可以使用抽象类型制作通用过程,例如:

伪参数的 fortran 类声明

但是我可以用下面的代码做同样的事情吗?

module proc_mod
  public :: forced,ideal
  interface forced;    module procedure forced1;    end interface
  interface forced;    module procedure forced2;    end interface
contains

function forced1() result(forced)
 implicit none
 integer:: forced
 forced = 1
end function

function forced2(t) result(forced)
 implicit none
 integer,intent(in) :: t
 integer:: forced
 forced = t
end function

function ideal() result(i)
 implicit none
 integer:: i
 i = 2
end function

end module

program procTest
  use proc_mod
  implicit none

  integer :: n

  procedure(forced), pointer:: funPointer => NULL()

  write(*,'(A)') "Please enter the type of vortex calculation you wish to use."
  read(*,*) n                                                                  

  select case( n )
    case( 1 );    funPointer => forced
    case( 2 );    funPointer => ideal
    case default; funPointer => ideal
  end select

  write(*,'(A,I3)') "You chose function: ", funPointer()

  stop
end program

现在我得到了错误: funPointer 可能不是通用的。我确信有办法解决它,但我不熟悉通用程序。

4

1 回答 1

2

不,您不能将过程指针指向通用过程。您也不能将通用过程作为虚拟参数传递。您必须始终选择一项特定功能。

您也许应该解释您的意图,而不仅仅是显示一些代码并询问是否可以以不同的方式完成。但无论如何,请记住 Fortran 泛型用于编译时调度。

于 2015-03-07T19:06:52.073 回答