私有,在Fortran 90模块中保存变量的属性 [英] Private, Save attributes for variables in Fortran 90 modules
问题描述
我试图在模块中添加对某些变量的访问限制(使用 private
属性),但是我需要在随后的调用中的例程中使用这些变量模块:
module MyMod
private
整数,allocatable :: A(:)
public :: init,calc
包含
子例程init()
!分配和初始化A
结束子程序init
子程序calc()
!使用A
结束子程序
结束模块
问题:
-
这是真的,私有变量不在
使用
这个模块。 $ c>保存属性为这个变量。如果我错了,请纠正我的错误? ol> Is this true that the private variable will not be in the scope of the program which
use
s this module.If the answer to 1 is yes, then I would think that I can use
save
attribute for this variable. Please correct me if I am wrong?Is this the proper way to perform this task?
是的,如果您将私有语句放入模块而无需进一步指定,则将默认辅助功能设置为私有
。
对于第一个问题,Fortran 2008 Standard(Cl.4.5.2.2§3)指出:
如果一个类型定义是
private
,那么只能访问该类型的类型名称以及结构构造函数(4.5.10)
所以 A
在包含定义的模块中,将无法从模块或子模块(后代)之外的任何地方访问。
对于第二个问题,是的 - 您可以在这里使用 save
。 (这与可访问性属性无关)。事实上,从Fortran 2008开始,这是隐含的模块变量,请参阅Fortran 2008 Standard(Cl。5.3.16§4)[thanks @francescalus]:
在主程序,模块或子模块的作用域单元中声明的变量,通用块或过程指针隐式具有SAVE属性,可以通过显式指定[ / p>
如果我正确理解你的第三个问题,它与初始化有关。你可以通过一个函数/子程序来实现这一点,你传递一个数组来初始化:
module MyMod
! ...
包含
! ...
子程序init(A_in)
隐式无
整数,意图(in):: A_in(:)
!分配和初始化A
分配(A(size(A_in)))
A = A_in
结束子程序
结束模块
$ c $在 init()
中创建一个 A_in <$ c $的副本。 $ b> / code>,它只能在模块中访问。只要 calc()
是该模块(或其子模块)的一部分,就可以完全访问 A
。
I am trying to add access restrictions to some of the variables (using private
attribute) in a module but I need to use those variables in subsequent invocations of routines within that module:
module MyMod
private
integer,allocatable :: A(:)
public :: init,calc
contains
subroutine init()
! allocating and initializing A
end subroutine init
subroutine calc()
! Using A
end subroutine
end module
Questions:
解决方案 Yes, if you put a private statement into your module without any further specification, you set the default accessibility to private
.
For the first question, the Fortran 2008 Standard (Cl. 4.5.2.2 §3) states that:
If a type definition is private
, then the type name, and thus the structure constructor (4.5.10) for the type, are accessible only within the module containing the definition, and within its descendants.
So A
will not be accessible from anywhere outside the module or submodule (descendant).
For the second question, yes - you can use save
here. (This is not related to the accessibility attribute). In fact, starting with Fortran 2008, this is implied for module variables, see for the Fortran 2008 Standard (Cl. 5.3.16 §4) [thanks @francescalus]:
A variable, common block, or procedure pointer declared in the scoping unit of a main program, module, or submodule implicitly has the SAVE attribute, which may be confirmed by explicit specification [...]
If I understood your third question correctly, it is related to the initialization. You could realize this with a function/subroutine to which you pass an array for initialization:
module MyMod
! ...
contains
! ...
subroutine init( A_in )
implicit none
integer, intent(in) :: A_in(:)
! allocating and initializing A
allocate( A(size(A_in)) )
A = A_in
end subroutine
end module
Inside init()
you create a copy of A_in
which is only accessible within the module. As long as calc()
is part of the module (or a submodule thereof), it has full access to A
.
这篇关于私有,在Fortran 90模块中保存变量的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!