意图(inout)和指针虚拟参数之间的区别
有什么实际区别
subroutine fillName(person)
type(PersonType), intent(inout) :: person
person%name = "Name"
end subroutine
拥有或以下
subroutine fillName(person)
type(PersonType), pointer :: person
person%name = "Name"
end subroutine
What is the practical difference in having
subroutine fillName(person)
type(PersonType), intent(inout) :: person
person%name = "Name"
end subroutine
or the following
subroutine fillName(person)
type(PersonType), pointer :: person
person%name = "Name"
end subroutine
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
pointer
具有裸描述所没有的特定参数要求。基本上,虚拟参数person
必须与指针目标关联。它可以通过分配或简单的指针赋值 (=>
) 来实现。需要注意的重要一点是,在子例程执行期间对虚拟参数 person 的指针关联的任何更改都将反映在传递的实际参数中。裸描述将通过引用传递实际参数,但不传递指针关联。pointer
has specific argument requirements that the bare description does not have. Basically the dummy argumentperson
must be associated with a pointer target. It could be through an allocation or simple pointer assignment (=>
). An important thing to note is that any changes to the pointer association of the dummy argumentperson
during the execution of the subroutine will be reflected in the actual argument passed. The bare description will pass the actual argument by reference, but not pointer association.如果我假设关键字实用,
那么您给出的示例中的实际差异将是可读性,因为它们都有效,但
intent(inout)
更明确。技术上的区别在于指针可能为 null 或不确定,而使用
intent(inout)
则必须分配变量。指针也可以在子例程中关联或无效,但带有intent(inout)
的虚拟参数则不能。如果您既没有指定
pointer
也没有指定intent(inout)
并且在参数中传递了一个指针,那么它必须关联起来。If I assume the keyword is practical,
then the practical difference in the example you give would be readability, since they both work but
intent(inout)
is more explicit.The technical difference is that the pointer may be null or undetermined, whereas with
intent(inout)
the variable have to be allocated. A pointer can also be associated or nullified in the subroutine but a dummy argument withintent(inout)
cannot.If you don't specify neither
pointer
orintent(inout)
and you pass a pointer in argument then it have to be associated.