代码之家  ›  专栏  ›  技术社区  ›  Flo Ragossnig

在Fortran中从父类继承赋值

  •  0
  • Flo Ragossnig  · 技术社区  · 6 年前

    对不起,又是我!

    尽管我在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')
    

    感谢您的帮助!

    1 回复  |  直到 6 年前
        1
  •  1
  •   Flo Ragossnig    6 年前

    因为似乎没有人知道答案,我也不知道如何解决这个问题 类型 我把这个“解决方法”贴在这里,以防有人有同样的问题。我只是把作业重载放到一个单独的模块中 使用

    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