从 Fortran 中的 parent class 继承赋值
Inherit assignment from parent class in Fortran
对不起,又是我!
尽管我在 Fortran 中的 OOP 越来越好(这可能是我曾经使用过的最疯狂的东西),但我在继承方面遇到了困难。不幸的是,我不明白允许我这样做的语法。
基本上,我想做的是覆盖赋值运算符 =
,它允许我 return 任何 原始类型 。只有一种原始类型 (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,因为它们允许内部赋值重载)。所以我现在的问题是如何在一个包含所有赋值重载(实数、整数、字符、逻辑)和在仅包含 func
的 child class 中使用此覆盖?在程序中我只想包含 child class 并用类似的东西赋值:
type(child_class) :: child
real :: var
var = child%func('real')
感谢任何帮助!
因为似乎没有人知道答案,我也不知道如何用 types 来解决这个问题,我 post 这个 "workaround" 以防万一有人有同样的问题。我只是将赋值重载放入一个单独的模块中,然后 在我需要的任何地方使用 该模块。一个简化的示例如下所示:
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) 中提取未知原始类型的参数。
对不起,又是我!
尽管我在 Fortran 中的 OOP 越来越好(这可能是我曾经使用过的最疯狂的东西),但我在继承方面遇到了困难。不幸的是,我不明白允许我这样做的语法。
基本上,我想做的是覆盖赋值运算符 =
,它允许我 return 任何 原始类型 。只有一种原始类型 (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,因为它们允许内部赋值重载)。所以我现在的问题是如何在一个包含所有赋值重载(实数、整数、字符、逻辑)和在仅包含 func
的 child class 中使用此覆盖?在程序中我只想包含 child class 并用类似的东西赋值:
type(child_class) :: child
real :: var
var = child%func('real')
感谢任何帮助!
因为似乎没有人知道答案,我也不知道如何用 types 来解决这个问题,我 post 这个 "workaround" 以防万一有人有同样的问题。我只是将赋值重载放入一个单独的模块中,然后 在我需要的任何地方使用 该模块。一个简化的示例如下所示:
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) 中提取未知原始类型的参数。