3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[Fortran] オブジェクト指向メモ

Last updated at Posted at 2019-06-29

コンパイラはifort.
単語帳.毎回検索するのが面倒なので転載多め.元URLあり.
自前の検証メモも.

#まとめ

[Qiita@bluepost59: fortranでもオブジェクト指向したい!]
(https://qiita.com/bluepost59/items/ca560c49a8c19484db9d)
[Qiita@bluepost59: 続・fortranでもオブジェクト指向したい!「継承と抽象クラス」]
(https://qiita.com/bluepost59/items/8d4b7d7713676fe06472)

以下列挙

メソッドに総称名を用いる

  • それぞれの手続き(個別手続き)を結合する
  • その手続きをgenericでまとめる
    ※クラスの外で総称名を定義し,それをクラスに結合する,という作戦は失敗
generic_method.f90
module mymod
implicit none

type :: mytype
    contains
    procedure :: mysub_i => mysub_i ! 必要
    procedure :: mysub_r => mysub_r ! 必要
    generic   :: mysub   => mysub_i, mysub_r
    !procedure :: mysub   => mysub ! 無意味
end type mytype

!interface mysub ! 無意味
!    module procedure mysub_i
!    module procedure mysub_r
!end interface mysub

contains

subroutine mysub_i(self, a)
    class(mytype), intent(in) :: self
    integer,       intent(in) :: a
    write(*, *) 'integer', a
end subroutine mysub_i

subroutine mysub_r(self, a)
    class(mytype), intent(in) :: self
    real(4),       intent(in) :: a
    write(*, *) 'real', a
end subroutine mysub_r
end module mymod

program main
    use mymod
    implicit none
    type(mytype) :: a
    call a%mysub(1)
    call a%mysub(2.0)
end program main

http://d.hatena.ne.jp/fortran66/20101111/1289407057
ここの文法だと今は上手く行かない?↓(class→typeにした時点でコンパイルエラー)
http://jjoo.sakura.ne.jp/tips/fortran2003/fortran2003_type_proc.html

旧バージョンifortでのバグ

ifortのバージョンが古いと(12.1.0以下)コンストラクタによるオーバーロードが出来ないというバグが存在する.
(コンパイルは出来るが,クラスをtype(MyClass)と呼び出すときにerror #6463: This is not a derived type name.と言われる.)
https://software.intel.com/en-us/forums/intel-fortran-compiler-for-linux-and-mac-os-x/topic/271265

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?