在一些Fortran 95代码中,我有一个带指针字段的类型。我想声明一个在编译时初始化的模块变量type(foo)
。类似这样的:是否有可能在Fortran的类型构造函数中使用指针?
module foo_module
implicit none
type foo_type
integer :: x
logical, pointer :: x_flag => null()
end type foo_type
logical, target :: bar_flag
! this does not compile of course:
type(foo_type) :: bar = foo_type(1, bar_flag)
end module foo_module
上面的代码片段不能编译。我知道我可以使用一个单独的子程序,像在运行时初始化bar
:
module foo_module
implicit none
type foo_type
integer :: x
logical, pointer :: x_flag => null()
end type foo_type
logical, target :: bar_flag
type(foo_type) :: bar
contains
subroutine init()
bar%x = 1
bar%x_flag => bar_flag
end subroutine init
end module foo_module
但有可能做到这一点没有初始化子程序?或者是否可以声明由编译器明确调用的初始化子例程?注意:这必须在Fortran 95中完成。
感谢您的出色解释。被限制为Fortran 95语言子集确实吸收:( – abbot 2012-08-10 20:30:21