【发布时间】:2020-12-17 13:51:43
【问题描述】:
如果模块foo 中私有的类型绑定过程被第二个模块bar 中的类型覆盖(或试图被覆盖),如何解决?标准中有规范吗?考虑到以下示例代码,取决于编译器,打印 FOO(intel fortan 19.1.1)或 BAR(gfortran 7.5,也许更新的版本会给出不同的结果?),我想知道哪个是正确的。
module foo
type :: foo_t
contains
procedure, private :: foobar
procedure :: exec
end type foo_t
contains
subroutine exec(obj)
class(foo_t) :: obj
call obj%foobar()
end subroutine exec
subroutine foobar(this)
class(foo_t) :: this
print *, "FOO"
end subroutine foobar
end module foo
module bar
use foo
type, extends(foo_t) :: bar_t
contains
procedure :: foobar => impl
end type bar_t
contains
subroutine impl(this)
class(bar_t) :: this
print *, "BAR"
end subroutine impl
end module bar
program test
use foo
use bar
class(foo_t), allocatable :: inst
allocate( bar_t :: inst)
call inst%exec()
end program test
此外,是否可以在不同的模块中使用私有延迟方法扩展抽象类型,就像示例中的情况一样,如果 foobar 被延迟(仅使用上述 gfortran 编译器编译然后产生预期的结果,但问题是这是否也是正确的行为)?
【问题讨论】:
标签: fortran polymorphism