我是否理解F2k3中没有虚拟析构函数?
stefanos-imac:oop borini$ cat a.f90
module AModule
type :: AType
contains
final :: A_dtor
end type
contains
subroutine A_dtor(self)
type(AType), intent(inout) :: self
print *, "A_dtor"
end subroutine
end
stefanos-imac:oop borini$ cat b.f90
module BModule
use AModule
type,extends(AType) :: BType
contains
final :: B_dtor
end type
contains
subroutine B_dtor(self)
type(BType), intent(inout) :: self
print *, "B_dtor"
end subroutine
end
stefanos-imac:oop borini$ cat x.f90
program x
use AModule
use BModule
class (AType), pointer :: baseptr
type(BType), pointer :: derivedptr
allocate(derivedptr)
baseptr => derivedptr
deallocate(baseptr)
end program
stefanos-imac:oop borini$ ./a.out
A_dtor
forrtl: severe (173): A pointer passed to DEALLOCATE points to an array that cannot be deallocated
Image PC Routine Line Source
a.out 0000000108A731F4 Unknown Unknown Unknown
a.out 0000000108A7198E Unknown Unknown Unknown
a.out 0000000108A4D791 Unknown Unknown Unknown
a.out 0000000108A2283E Unknown Unknown Unknown
a.out 0000000108A3B930 Unknown Unknown Unknown
a.out 0000000108A1EF10 Unknown Unknown Unknown
a.out 0000000108A0A104 Unknown Unknown Unknown
a.out 0000000108A09F0C Unknown Unknown Unknown
a.out 0000000108A09EC4 Unknown Unknown Unknown
答案 0 :(得分:4)
我对您使用的C ++或Java术语(虚拟)不太确定。在谈到Fortran时,我更喜欢使用Fortran术语。在Fortran术语中,终止过程不会被继承,也不能被覆盖。应该执行新类型和扩展类型的终结过程。
据我所知,即使我没有对它有良好支持的编译器,你的程序似乎也是正确的。我希望输出:
B_dtor
A_dtor
不要对某些特定编译器的Fortran 2003和2008标准做出假设,因为还没有真正的Fortran 2003编译器,尽管他们中有几个假装是这样的。英特尔Fortran AFAIK并未声称完全符合Fortran 2003标准。