OOP Fortran:保存指向意图(IN)变量的指针
我有一个Fortran模块,我希望尽可能按照OOP原理组织它,同时使它与Fortran 2003兼容。这个模块基本上是:(a)分配/释放临时数组缓冲区,(b)提供一个对某些数据进行操作的函数do_F。此函数不使用这些临时缓冲区,但也依赖于几种辅助类型 我很清楚,我应该将缓冲区放入一个类型中,并在适当的时候初始化/释放。然而,由于对do_F的每次调用都需要几个参数,因此我确信使用什么样的设计策略是最好的OOP Fortran:保存指向意图(IN)变量的指针,fortran,fortran90,fortran95,fortran2003,Fortran,Fortran90,Fortran95,Fortran2003,我有一个Fortran模块,我希望尽可能按照OOP原理组织它,同时使它与Fortran 2003兼容。这个模块基本上是:(a)分配/释放临时数组缓冲区,(b)提供一个对某些数据进行操作的函数do_F。此函数不使用这些临时缓冲区,但也依赖于几种辅助类型 我很清楚,我应该将缓冲区放入一个类型中,并在适当的时候初始化/释放。然而,由于对do_F的每次调用都需要几个参数,因此我确信使用什么样的设计策略是最好的 更具体地,考虑以下实现: 每次调用do\u F时都要传递大量类型 type object_t
更具体地,考虑以下实现:
type object_t
! lots of private buffers
real, allocatable :: buf1(:,:,:), buf2(:,:,:), etc.
end type object_t
subroutine init_object(this)
type(object_t), intent(INOUT) :: this
allocate( this%buf1(..., ..., ...) )
!...
end subroutine init_object
subroutine do_F(this, data, aux1, aux2, ..., auxN)
type(object_t), intent(INOUT) :: this
type(data_t), intent(INOUT) :: data
type(aux1_t), intent(IN) :: aux1
!...
!do stuff on data using the buffers and values stored
! in aux1 .. auxN
end subroutine do_F
type object_t
! lots of private buffers
real, allocatable :: buf1(:,:,:), buf2(:,:,:), etc.
! pointers to auxiliary types
type(aux1_t), pointer :: aux1_ptr
!...
end type object_t
subroutine init_object(this, aux1, aux2, ..., auxN)
type(object_t), intent(INOUT) :: this
type(aux1_t), intent(IN), target :: aux1
!...
allocate( this%buf1(..., ..., ...) )
!...
this%aux1_ptr => aux1
!...
end subroutine init_object
subroutine do_F(this, data)
type(object_t), intent(INOUT) :: this
type(data_t), intent(INOUT) :: data
!do stuff on data using the buffers and values stored
! in this%aux1_ptr .. this%auxN_ptr
end subroutine do_F
谢谢 如果在调用之前,变量中的Intent在返回后会得到很好的定义。程序不允许更改它们。指针变量的值是一个例外,您可以更改目标的值,但不能更改
intent(IN)
指针伪参数的指针关联状态
不过,我不确定效率如何。快速阅读后,版本2看起来更好。多亏了downvoter。实际上我已经找了很长时间来修复它,但是我找不到它。感谢伊恩指出了真实的状态。