我有一个对象解析文本文件。这是我的主要程序:如何在Fortran中正确定位对象?
program main
use Parser_class
implicit none
type(Parser) :: Parser
call Parser%ProcessFile('data.txt')
call Parser%Deallocate
end program main
其中类型定义
module Parser_class
type :: Parser
contains
procedure, public :: ProcessFile
procedure, public :: Deallocate
end type Parser
contains
subroutine ProcessFile(self)
...
end subroutine
subroutine Deallocate(self)
class(Parser) :: self
...
end subroutine
end module Parser_class
我读到最后的关键字,并改变了类型定义
module Parser_class
type :: Parser
contains
procedure, public :: ProcessFile
final :: Deallocate
end type Parser
contains
subroutine ProcessFile(self)
...
end subroutine
subroutine Deallocate(self)
type(Parser) :: self
...
end subroutine
end module Parser_class
此外,在主程序我再也没有call Parser%Deallocate
了。终结者现在不会在任何时候被调用。我以某种方式得到这是因为我从不销毁或覆盖Parser
对象。但是,我怎么能做到这一点,或者什么是处理释放过程的正确方法?
我加了'end program'。该程序按预期工作(只读取文本文件)。我只是想知道,如果我使用'调用Parser%Deallocate'的方式是释放所有数组的正确方法,或者我应该使用终结器来完成。另外的问题是什么时候确切地敲定了终结者。虽然不能提供一个工作示例,但我对O-O Fortran还是比较陌生的。 – THo