【发布时间】:2019-07-20 13:53:20
【问题描述】:
我刚开始在 Fortran 2003 中重载运算符(包括赋值),我想为我的用户定义类型重载箭头运算符 (=>)。 我知道对于大多数运算符,比如 (+),我会说
interface operator(+)
! What I want this to mean instead
end interface operator
但是,这不适用于 (=>)。我知道分配,我会说
interface assignment(=)
! What I want this to mean instead
end interface assignment
这仍然不适用于 (=>)。
具体来说,我定义了一个基础数据是指针的类型。
type my_type
integer, pointer :: data(:)
end type my_type
所以,当我说
type (my_type) :: a
integer, target :: b(4)
! Do stuff to b
a => b
我想说的是
a%data => b
感谢您的任何建议! 2003 标准以外的标准中的答案也会有所帮助。
【问题讨论】:
-
谢天谢地,您似乎无法为所欲为。在你上面的例子中,如果声明是
type(my_type), pointer :: a。a => b是什么意思?这是语法错误吗? -
请注意
=>isn't an operator。这里的问题涉及=不是运算符,因此值得强调的是指针赋值也不是一个运算符。 -
@Steve 我希望在我对答案的评论中引用你。有兴趣的请看下文。我习惯于在 C++ 中显式取消引用指针,所以您的评论肯定有助于我更好地理解 Fortran。
-
@francescalus 感谢您让我意识到在 Fortran 中没有赋值被视为运算符。这很有意义,为什么我不能进行链式分配,因为它们不返回任何东西。
标签: pointers fortran operator-overloading overloading