我想将指向函数的指针存储为具有类型的属性。但编译器抱怨函数的第一个参数应该是类型(如Python 或 C++ 等语言中的self
或this
参数)。但属性只是一个指针 - 它不是类型的一部分。
以下是一些代码:
module my_mod
implicit none
public :: my_type
abstract interface
! INTERFACE I WANT TO POINT TO
subroutine my_interface(arg)
real, intent(in) :: arg
end subroutine my_interface
end interface
type :: my_type
! MY TYPE DEFINITION
real :: my_variable
procedure(my_interface), pointer :: my_fn_pointer
end type my_type
interface my_type
! "CONSTRUCTOR" INTERFACE FOR MY TYPE
module procedure init_my_type
end interface
contains
type(my_type) function init_my_type(arg1, arg2)
! ACTUAL "CONSTRUCTOR" FOR MY TYPE
real, intent(in) :: arg1
procedure(my_interface) :: arg2
init_my_type%my_variable = arg1
init_my_type%my_fn_pointer => arg2
end function init_my_type
end module my_mod
我得到的实际错误(ifort 编译器)是:
错误 #8262:对于具有 PASS 绑定属性的类型绑定过程,第一个虚拟参数必须具有与所定义类型相同的声明类型。
我不明白为什么它认为我想要指向的接口是一个类型绑定的过程。
有什么提示吗?
您被错误信息的措辞误导了,因为它与类型绑定程序有关。这是错误信息本身的问题,它不明确。
您的派生类型没有绑定。
但是,“传递对象伪参数”概念既适用于过程指针组件(您确实有),也适用于类型绑定过程。
NOPASS
如果您想避免传递对象虚拟,则应该在使用过程指针组件和类型绑定过程时使用该属性。