4

Fortran で拡張された型を使用すると、別のモジュールの型拡張から見えるようにプライベート コンポーネントが必要になります。

gcc4.7 と ifort の両方で、bName が初期型と拡張子の両方にあるため、次のコードはエラーになります。しかし、これは非公開であるため、別のモジュールの拡張ではアクセスできません。つまり、bar_type の bName をコメントアウトすると、非公開であるというエラーが発生します。

module foo
  type :: foo_type
    character(256),private :: bName = "foo"
  contains
    procedure :: pName => pName
  end type
contains
  subroutine pName(this)
    class(foo_type), intent(in) :: this
    print*,trim(this%bName)
  end subroutine
end module

module bar
  use foo, only : foo_type
  type,extends(foo_type) :: bar_type
    character(256),private :: bName = "bar"
  contains
    procedure :: pName => pName
  end type
contains

  subroutine pName(this)
    class(bar_type), intent(in) :: this
    print*,this%bName
  end subroutine
end module


program test
  use foo, only : foo_type
  use bar, only : bar_type
  type(foo_type) :: foo_inst
  type(bar_type) :: bar_inst

  call foo_inst%pName()
  call bar_inst%pName()
end program

bar_type が foo_type と同じモジュールに含まれている場合、bName は bar_type からアクセスできます。つまり、次のコードがコンパイルされます。

module foo
  type :: foo_type
    character(256),private :: bName = "foo"
  contains
    procedure :: pName => pName
  end type

  type, extends(foo_type) :: baz_type
  contains
    procedure :: pName => pName_baz
  end type
contains
  subroutine pName_baz(this)
    class(baz_type), intent(in) :: this
    print*,trim(this%bName)
  end subroutine

  subroutine pName(this)
    class(foo_type), intent(in) :: this
    print*,trim(this%bName)
  end subroutine
end module

program test
  use foo, only : foo_type,baz_type
  type(foo_type) :: foo_inst
  type(baz_type) :: baz_inst

  call foo_inst%pName()
  call baz_inst%pName()
end program

最初の例で何が起こるべきかを知るために、標準を解析するのに苦労しています。

4

1 に答える 1

2

最初の例は標準に準拠していないと思います。

private 属性により、コンポーネントはbNamemodule の外からアクセスできなくなりますが、fooそれでも継承されますbar_type(何もできないため、おそらく無意味ですが、それは問題ではありません) -- f2003 の注 4.51 を参照してください。

親型のアクセスできないコンポーネントとバインディングも継承されますが、拡張型ではアクセスできません。拡張される型が使用アソシエーションを介してアクセスされ、プライベート エンティティがある場合、アクセスできないエンティティが発生します。

そのbar_typeため、その名前bNameで別のコンポーネントを追加するとエラーになります (スコープと名前の規則については、段落 16.2 を参照してください)。

于 2012-07-03T00:54:06.233 に答える