2

Do I understand correctly that there are no virtual destructors in 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
4

1 回答 1

4

我不太确定您使用的 C++ 或 Java 术语(虚拟)。在谈到 Fortran 时,我更喜欢使用 Fortran 术语。在 Fortran 术语中,终结过程不会被继承,也不能被覆盖。应该执行新类型和扩展类型的终结过程。

据我了解这个问题,即使我没有一个很好的支持它的编译器,你的程序似乎是正确的。我希望输出:

B_dtor
A_dtor

不要假设 Fortran 2003 和 2008 标准形成了某些特定的编译器,因为目前还没有真正的 Fortran 2003 编译器,尽管其中有几个假装是。英特尔 Fortran AFAIK 并未声称完全符合 Fortran 2003。

于 2012-06-14T14:48:14.087 回答