텍스트 파일을 구문 분석하는 개체가 있습니다. 여기 내 주요 프로그램 :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
final :: Deallocate
end type Parser
contains
subroutine ProcessFile(self)
...
end subroutine
subroutine Deallocate(self)
type(Parser) :: self
...
end subroutine
end module Parser_class
에 유형 정의를 변경
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
입니다 I 더 이상 call Parser%Deallocate
이 없습니다. 이제 파이널 라이저는 언제든지 호출되지 않습니다. 나는 어쨌든 Parser
객체를 파괴하거나 겹쳐 쓰지 않기 때문에 이것을 얻는다. 그러나 어떻게 이것을 할 수 있습니까? 또는 할당 해제 프로세스를 처리하는 적절한 방법은 무엇입니까?
'최종 프로그램'을 추가했습니다. 이 프로그램은 의도 한대로 작동합니다 (텍스트 파일을 읽음). 나는'call Parser % Deallocate '를 사용하는 나의 방법이 모든 배열을 할당 해제하는 올바른 방법인지 아니면 파이널 라이저로해야 하는지를 알고 싶다. 추가 질문은 finalizer가 정확히 호출 될 때입니다. 그래도 실제 예제를 제공 할 수는 없지만, 저는 O-O Fortran을 처음 접했습니다. – THo