小编Bál*_*adi的帖子

具有重载赋值的嵌套派生类型

我有一个wrapper包含其他派生类型(over)的派生类型().对于后者,赋值运算符已经过载.由于派生类型的赋值按默认的组件方式发生,我希望分配两个实例wrapperover在某个时刻调用重载的赋值.但是,使用下面的程序似乎并非如此.仅当我还重载用于在实例之间wrapper包含显式赋值的赋值时over(通过取消注释注释的代码行),才会调用重载的赋值.为什么?我发现它有点违反直觉.有没有办法避免包装类型的过载?

module test_module
  implicit none

  type :: over
    integer :: ii = 0
  end type over

  type :: wrapper
    type(over) :: myover
  end type wrapper

  interface assignment(=)
    module procedure over_assign
    !module procedure wrapper_assign
  end interface assignment(=)

contains

  subroutine over_assign(other, self)
    type(over), intent(out) :: other
    type(over), intent(in) :: self

    print *, "Assignment of over called"
    other%ii = -1

  end subroutine over_assign

  !subroutine wrapper_assign(other, self)
  !  type(wrapper), intent(out) …
Run Code Online (Sandbox Code Playgroud)

fortran fortran95 fortran2003

7
推荐指数
1
解决办法
1363
查看次数

Fortran 2003 中的常量函数指针数组

亲爱的 Fortran 程序员,

有人知道,是否可以在 Fortran 2003 或更高版本中声明一个常量(参数)过程指针数组?

如下所示,我有一个切换器函数,它根据传入的整数参数调用不同的函数。它使用一组过程指针(包装在派生中)类型。该数组必须init()运行时通过例程初始化,然后才能使用。有什么办法可以在编译期间初始化这个数组并避免这种初始化例程的必要性?它也可以定义为parameter,因为它的值在运行期间不会改变。

module testmod
  implicit none

  interface
    function funcInterface() result(res)
      integer :: res
    end function funcInterface
  end interface

  type :: ptrWrap
    procedure(funcInterface), nopass, pointer :: ptr
  end type ptrWrap

  type(ptrWrap) :: switcher(2)

contains

  subroutine init()
    switcher(1)%ptr => func1
    switcher(2)%ptr => func2
  end subroutine init

  function callFunc(ii) result(res)
    integer, intent(in) :: ii
    integer :: res
    res = switcher(ii)%ptr()
  end function callFunc

  function func1() result(res)
    integer :: res …
Run Code Online (Sandbox Code Playgroud)

fortran fortran2003 fortran2008

5
推荐指数
1
解决办法
314
查看次数

标签 统计

fortran ×2

fortran2003 ×2

fortran2008 ×1

fortran95 ×1