从Fortran的父类继承分配

时间:2018-10-09 14:43:55

标签: oop fortran assignment-operator

对不起,我再次!

即使我在Fortran中使用OOP变得越来越好(这可能是我曾经使用过的最疯狂的东西),但继承还是有困难。不幸的是,我不理解允许我这样做的语法。

基本上,我想做的是覆盖赋值运算符=,这使我可以返回任何原始类型。一个只有一个原始类型(real)的基本示例如下所示:

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,因为它们允许固有分配重载)。因此,我的问题是现在我如何在一个包含所有分配重载(实数,整数,字符,逻辑)的单独模块中定义一个父类,并在子类< / strong>仅包含func?在该程序中,我只想包含子类,并使用类似以下内容的值来赋值:

type(child_class) :: child
real :: var

var = child%func('real')

任何帮助表示赞赏!

1 个答案:

答案 0 :(得分:1)

由于似乎没人知道答案,我也不知道如何使用类型解决这个问题,如果有人遇到相同的问题,我会在此发布“解决方法”。我只是将分配重载放在一个单独的模块中,并在需要的地方使用。一个简化的示例如下所示:

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)中提取未知原始类型的参数。