2012-06-29 31 views
4

对于Fortran中的扩展类型,应通过对不同模块中的类型扩展可见的私有组件。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 

一直有一个很难解析标准就知道在第一个例子中应该发生什么。

+0

嗨,杰里米。我有完全相同的问题。你有没有想过如何在不同的模块中分离基类和派生类型以及访问派生成员中的私有基类成员?谢谢。 – FortCpp

回答

2

我相信第一个例子是不符合标准的。

即使私有属性使得bName无法访问外模块foo,它仍然是由bar_type继承了组件(也许,而不是毫无意义的,因为没有什么可以用它做,但是这不是问题) - 见注4.51在F2003 :

父类型的不可访问组件和绑定也是继承的,但它们在扩展类型中仍然不可访问 。如果要扩展的类型通过 使用关联访问且具有私有实体,则会发生无法访问的实体。

所以bar_type有名为bName,这使得它的错误该名称(见范围和名称规则第16.2)要添加其他成分的继承组件。