【发布时间】:2012-06-14 22:48:14
【问题描述】:
我是否正确理解 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
【问题讨论】:
-
什么是虚拟析构函数?
-
@HighPerformanceMark :当你通过基指针删除一个对象时,它也会调用派生类型的析构函数。
-
这正是 Fortran 中应该发生的事情。
-
@VladimirF: 好吧,所以这可能是一个 ifort 错误?
-
我尝试使用英特尔编译器的最新版本 (12.1.4.325) 编译您的示例。按原样编译它,编译器因内部错误而崩溃。但是,当我为每种类型添加一个组件(只是一个整数)时,代码会编译并且运行时会打印“A_Dtor”
标签: fortran fortran2003