【发布时间】:2016-11-18 12:52:01
【问题描述】:
我有一个解析文本文件的对象。这是我的主程序:
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
我阅读了 final 关键字并将类型定义更改为
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。该程序按预期工作(仅读取文本文件)。我只想知道我使用call Parser%Deallocate的方式是否是释放所有数组的正确方式,或者我是否应该使用终结器来实现。另一个问题是何时调用终结器。虽然无法提供一个工作示例,但我对 O-O Fortran 还是很陌生。