有没有办法自动初始化过程指针的常量数组?
我有一堆例程,必须根据整数变量的值来调用它们。我不想使用select case
语句,而是想使用下面给出的过程指针。但是,如果我可以跳过过程指针数组的显式初始化,并将其定义为包装过程指针的常量数组,那就太好了。下面的代码演示了我找到的解决方案,注释行表示目标,我想实现:
module testmod
implicit none
abstract interface
subroutine subInterface()
end subroutine subInterface
end interface
type :: SubPtr
procedure(subInterface), nopass, pointer :: ptr
end type SubPtr
! Would be nice to use something like this:
!type(SubPtr), parameter :: subs(2) = [ SubPtr(sub1), SubPtr(sub2) ]
contains
subroutine sub1()
print *, "SUB1"
end subroutine sub1
subroutine sub2()
print *, "SUB2"
end subroutine sub2
end module testmod
program test
use testmod
implicit none
type(SubPtr) :: subs(2)
integer :: ii
! Would be nice to get rid of those two initialization lines
subs(1) = SubPtr(sub1)
subs(2) = SubPtr(sub2)
! Testing procedure pointer array
do ii = 1, 2
call subs(ii)%ptr()
end do
end program test