在一些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