gpt4 book ai didi

fortran - 具有私有(private)组件的结构构造函数

转载 作者:行者123 更新时间:2023-12-04 02:19:45 24 4
gpt4 key购买 nike

我在下面的例子中定义了一个带有私有(private)组件的结构:

module mtypes
implicit none

type mytype
integer, private :: nr
end type

end module mtypes

program main

use mtypes
type(mytype) :: t1

t1 = mytype(1)
print *, t1

end program main

以 1 作为参数调用结构构造函数。据我了解,这应该是不可能的,因为 nr 是私有(private)的。然而,这是由 Intel(R) Visual Fortran Compiler XE 14.0.5.239 [IA-32] 编译的,而不是 gfortran 4.9.3 使用 cygwin。此外,print 语句在输出中显示了 nr 的值。使用最新标准以这种方式定义具有私有(private)组件的结构是否有效 Fortran?或者这是英特尔编译器的错误?

最佳答案

在Fortran 2008的草案中,隐式(即派生类型定义而来的)结构构造器在4.5.10中有详细说明。给定的一个约束是

The type name and all components of the type for which a component-spec appears shall be accessible in the scoping unit containing the structure constructor.

由于组件 nr 在主程序中不可访问,因此将 nr 作为组件规范违反了此约束。在模块中使用此隐式结构构造函数会很好,私有(private)组件具有默认初始化也是如此。

nagfor 也因此提示您的代码示例。

print 语句中,这显然是无效的,ifort 15 对此进行了提示。要使 t1 出现在输出列表中,您必须使用定义输出过程。

关于fortran - 具有私有(private)组件的结构构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31452505/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com