我正在尝试将过程指针传递给派生类型绑定过程
module mymodule
use mystoremodule
implicit none
private
type, abstract, public :: mytype
contains
procedure :: parse
end type mytype
contains
subroutine parse(self,mypointer)
implicit none
! Declaring Part
class(mytype) :: self
procedure(storing),pointer, intent(in) :: mypointer
integer :: myvalue
! Executing Part
myvalue = 42
call mypointer(myvalue)
end subroutine parse
end module mymodule
其中storing
在另一个模块/派生类型中定义
module mystoremodule
implicit none
type, public :: storingtype
integer :: myvalue
contains
procedure, public :: storing
end type storingtype
contains
subroutine storing(self,myvalue)
! Declaring part
class(storingtype) :: self
integer, intent(in) :: myvalue
! Executing part
self%myvalue = myvalue
end subroutine SetExcitationOrder
end module mystoremodule
我调用程序
call mytypeobject%parse(storingtypeobject%storing)
有了它,我得到一个编译器错误
The type of the actual argument differs from the type of the dummy argument.
我发现错误来自过程指针未将虚拟参数传递给storing
过程(我没有将任何东西定义为nopass
(。在所有其他情况下,虚拟参数会自动传递,为什么不在这里?声明虚拟参数对我来说是不可行的,因为该过程使用的对象会发生变化。我的问题有什么解决方案吗?
你没有在那里传递一个过程指针。 storingtypeobject%storing
不是过程指针,而是对类型绑定过程的绑定,而不是任何类型的指针。
我实际上不会接受指针,而只是 parse
中的一个过程参数。
像这样:
subroutine parse(self,mypointer)
implicit none
! Declaring Part
class(mytype) :: self
procedure(storing), intent(in) :: myproc
integer :: myvalue
! Executing Part
myvalue = 42
call myproc(myvalue)
end subroutine parse
和
call mytypeobject%parse(storing_wrapper)
contains
subroutine storring_wrapper(myvalue)
integer, intent(in) :: myvalue
call storingtypeobject%storing(myvalue)
end subroutine
我认为过程指针只有在可以在某处更改时才最有用。不一定是parse
但在某些情况下需要将其设置为其他目标。