首页 > 解决方案 > 从 Fortran 中的父类继承分配

问题描述

对不起,又是我!

尽管我在 Fortran 中的 OOP 越来越好(这可能是我用过的最疯狂的事情),但我在继承方面遇到了困难。不幸的是,我不明白允许我这样做的语法。

基本上,我想要做的是覆盖=允许我返回任何原始类型的赋值运算符。只有一种原始类型(真实)的基本示例如下所示:

module overload

    implicit none

    public func, assignment(=)

    interface assignment(=)
        module procedure equalAssignmentReal
        !! additional procedures for integer, character, logical if neccessary
    end interface

contains

    subroutine equalAssignmentReal(lhs, rhs)      !! <-- all these subroutines should be in the parent class

        implicit none

        real,     intent(out) :: lhs
        class(*), intent(in)  :: rhs

        select type(rhs)
            type is (real)
                lhs = rhs
        end select

        return

    end subroutine equalAssignmentReal

    function func(string) result(res)      !! <-- I want this function in the child class

        implicit none

        character(len=*), intent(in) :: string
        class(*), allocatable        :: res

        if (  string == "real" ) allocate(res, source=1.0)

        return

    end function func

end module overload

program test

    use overload

    implicit none

    real :: var

    var = func('real')

    print *, "var = ", var

end program test

这在使用GNU Fortran编译时有效(不适用于 Intel,因为它们允许内部赋值重载)。所以我现在的问题是如何在包含所有赋值重载(实数、整数、字符、逻辑)的单独模块中定义父类,并在仅包含的子类func中使用此覆盖?在程序中,我只想包含子类并使用以下内容分配值:

type(child_class) :: child
real :: var

var = child%func('real')

任何帮助表示赞赏!

标签: oopfortranassignment-operator

解决方案


由于似乎没有人知道答案,而且我不知道如何使用类型来解决问题,所以我在这里发布了这个“解决方法”,以防有人遇到同样的问题。我只是将赋值重载放到一个单独的模块中,并在需要的地方使用该模块。一个简化的示例如下所示:

module overload

    implicit none

    public assignment(=)

    interface assignment(=)
        module procedure equalAssignmentReal
        module procedure equalAssignmentInteger
        !! additional procedures for character, logical if neccessary
    end interface

contains

    subroutine equalAssignmentReal(lhs, rhs)

        implicit none

        real,     intent(out) :: lhs
        class(*), intent(in)  :: rhs

        select type(rhs)
            type is (real)
                lhs = rhs
        end select

        return

    end subroutine equalAssignmentReal

    subroutine equalAssignmentInteger(lhs, rhs)

        implicit none

        integer,  intent(out) :: lhs
        class(*), intent(in)  :: rhs

        select type(rhs)
            type is (integer)
                lhs = rhs
        end select

        return

    end subroutine equalAssignmentInteger

end module overload


module assignment

    implicit none

    public find
    public par1

    real    :: par1
    integer :: par2

contains

    subroutine init

        use overload

        implicit none


        par1 = find('real')
        par2 = find('integer')        

        return

    end subroutine init

    function find(in) result(out)

        implicit none

        character(len=*), intent(in)  :: in
        class(*), allocatable         :: out

        if ( in == 'real' ) then

            allocate(out, source=1.)

        else if ( in == 'integer' ) then

            allocate(out, source=2)

        end if 

        return

    end function find

end module assignment

program test

    use assignment

    implicit none

    call init

    print *, "par1 = ", par1
    print *, "par2 = ", par2

end program test

我用它从文件(json)中提取未知原始类型的参数。


推荐阅读