【问题标题】:How to override user-defined I/O procedures?如何覆盖用户定义的 I/O 过程?
【发布时间】:2018-06-07 09:13:31
【问题描述】:

我有一个抽象类,其中包含用于未格式化二进制流的读/写方法。我也有一些从抽象类继承的类,其中一些还有我还想序列化的附加组件。

因此,我希望第一组方法用作“默认”行为,并且在继承的类中覆盖它的方法仅由这些特定类使用。

我试过这样实现它:

module m
    implicit none

    type, abstract :: a
        integer, public :: num
    contains
        procedure :: write_a
        procedure :: read_a

        generic            :: write(unformatted) => write_a
        generic            :: read(unformatted)  => read_a
    end type a

    type, extends(a) :: b
        integer, public :: num2
    contains
        procedure :: write_b
        procedure :: read_b

        generic :: write(unformatted) => write_b
        generic :: read(unformatted) => read_b
   end type b

    type, extends(a) :: c
    end type c

contains

    subroutine write_a(this, unit, iostat, iomsg)
        class(a), intent(in)    :: this
        integer, intent(in)         :: unit
        integer, intent(out)        :: iostat
        character(*), intent(inout) :: iomsg

        write(unit, iostat=iostat, iomsg=iomsg) this%num
    end subroutine write_a

    subroutine read_a(this, unit, iostat, iomsg)
        class(a), intent(inout) :: this
        integer, intent(in)         :: unit
        integer, intent(out)        :: iostat
        character(*), intent(inout) :: iomsg

        read(unit, iostat=iostat, iomsg=iomsg) this%num
    end subroutine read_a

    subroutine write_b(this, unit, iostat, iomsg)
        class(b), intent(in)    :: this
        integer, intent(in)         :: unit
        integer, intent(out)        :: iostat
        character(*), intent(inout) :: iomsg

        write(unit, iostat=iostat, iomsg=iomsg) this%num, this%num2
    end subroutine write_b

    subroutine read_b(this, unit, iostat, iomsg)
        class(b), intent(inout) :: this
        integer, intent(in)         :: unit
        integer, intent(out)        :: iostat
        character(*), intent(inout) :: iomsg

        read(unit, iostat=iostat, iomsg=iomsg) this%num, this%num2
    end subroutine read_b
end module m

program mwe
    use m

    implicit none

    class(a), allocatable :: o1, o2, o3

    o1 = b(1,2)
    o2 = c(3)

    open(123, file='test5.dat', form='unformatted')
        write(123) o1
    close(123)

    allocate(b :: o3)

    open(123, file='test5.dat', form='unformatted')
        read(123) o3
    close(123)

    write(*,*) o3%num, o3%num2
end program mwe

但我收到以下错误:

test5.f90(29): error #8638: The type/rank signature for arguments of this specific subroutine is identical to another specific subroutine that shares the same defined I/O.   [WRITE_A]
    subroutine write_a(this, unit, iostat, iomsg)
---------------^
test5.f90(86): error #6460: This is not a field name that is defined in the encompassing structure.   [NUM2]
    write(*,*) o3%num, o3%num2
--------------------------^

在我看来,就像 a 类中的 write 方法一样不能被覆盖。如何正确实施?

【问题讨论】:

    标签: class fortran overriding fortran2008


    【解决方案1】:

    这不是完全与定义的输入/输出过程有关的问题,而是更广泛的泛型绑定和类型绑定过程。

    您的类型 b 具有类型绑定过程,就好像(通过扩展)它被定义为一样

    type b
      integer, public :: num, num2
     contains
        procedure :: write_a, write_b
        procedure :: read_a, read_b
        generic   :: write(unformatted) => write_a, write_b
        generic   :: read(unformatted)  => read_a, read_n
    end type
    

    write_awrite_b 的绑定确实是模棱两可的(read_aread_b 也是如此)。 [其他地方的更多细节。]

    你并不真的需要那些 write_aread_a 绑定,所以它们应该被覆盖:

    type, abstract :: a
        integer, public :: num
     contains
        procedure :: write => write_a
        procedure :: read => read_a
    
        generic   :: write(unformatted) => write
        generic   :: read(unformatted)  => read
    end type a
    
    type, extends(a) :: b
        integer, public :: num2
     contains
        procedure :: write => write_b
        procedure :: read => read_b
    end type b
    

    这里b 类型的writeread 绑定覆盖a 类型的绑定。 write(unformatted)read(unformatted) 的通用绑定保留了到 b 中(现在已被覆盖)绑定的映射。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-06-26
      • 2023-03-21
      • 1970-01-01
      • 1970-01-01
      • 2021-07-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多