我遇到了新旧Fortran代码的一些问题。新部分是面向对象的,旧部分使用函数指针。
我的问题是,我想将一个成员函数赋值给一个函数指针,以便该函数可以使用这个特殊对象。以下是具有相同错误的示例代码:
module test
! abstract class
type, abstract :: base
contains
procedure (doSth), deferred :: doSomething
end type base
! deferred function
abstract interface
subroutine doSth(this)
import :: base
class(base) :: this
end subroutine
end interface
! derived class
type, extends(base) :: child
contains
procedure :: doSomething => do_Sth
end type child
contains
! deferred function implemented by child
subroutine do_Sth(this)
class(child) :: this
! ...
! ...
end subroutine
! function pointer to member function
subroutine get_functionPointer()
procedure() , pointer :: funcPtr
type (child), pointer :: childPtr
allocate (childPtr)
funcPtr => childPtr%doSomething
! ... This does not work
end subroutine
end module
这给了我错误信息:
error #8191: The procedure target must be a procedure or a procedure pointer.
有没有可能克服这个问题?
答案 0 :(得分:0)
在一些帮助下,这似乎有效:
:
:
! function pointer to member function
subroutine get_functionPointer()
procedure(doSth), pointer :: funcPtr
type (child), pointer :: childPtr
allocate (childPtr)
call funcPtr(childPtr)
end subroutine
:
:
非常感谢你的帮助。