问题描述
我想要一个派生类型, a
,它是空的。从这个派生类型中,我想定义扩展a的更多类型。假设所有这些类型的扩展都包含一些通用
过程名称,值
,即 value => ; valuea1
, value => valuea2
等。
如果我想将类a的变量传递给其他过程,我需要声明相关的伪参数该过程使用 class(a)
。然而,如果我这样做了,那么引用dummy参数的值
会导致编译失败,因为类a实际上是空的 - 只有类型扩展包含该过程。 p>
我可以想到通过在a的类型定义中有一些名为 value
的过程来解决这个问题(然后在扩展中覆盖)。然而,鉴于我从来不想声明任何类型为a的对象,这看起来很乱。它有可能解决这个问题吗?解决方案是的,你可以声明一个类型绑定过程,即使是一个摘要
类型。它可以是一个真正的类型绑定过程,或者只是一个抽象接口
。
type,abstract :: a
包含
procedure :: valuea1,valuea2
generic value :: value => valuea1,valuea2
结束类型
抽象接口
! valuea1,valuea2的标题
!他们应该有一个通过的虚拟参数类(a)
!和一些其他的通用分辨率
的参数!例如:
子程序valua1(self,x)
class(a),intent(in):: self
real,intent(inout):: x
结束子程序
子程序valua2(self,x)
class(a),intent(in):: self
integer,intent(inout):: x
结束子程序
结束界面
这样你就不能创建 type(a)
,但是你可以扩展类型来实现他们自己版本的 value
。
I would like to have a derived type, a
, which is empty. From this derived type I would like to define further types which extend a. Suppose all of these type extensions contain some generic
procedure name, value
, i.e value => valuea1
, value => valuea2
, etc.
If I then want to pass variables of class a to some other procedure, I need to declare the relevant dummy argument of that procedure with class(a)
. If I do this, however, then referencing the value
of the dummy argument leads to compilation failure because the class a is actually empty - only the type extensions contain the procedure.
I could presumably get around this by having some procedure called value
inside the type definition of a (then overriding in the extensions). However, given that I never want to declare any object with type a, this seems messy. It it possible to get around this?
Yes, you can declare a type bound procedure even for an abstract
type. It can be a real type bound procedure, or just an abstract interface
.
type, abstract :: a
contains
procedure :: valuea1, valuea2
generic value :: value => valuea1, valuea2
end type
abstract interface
! the headers of valuea1, valuea2 here
! they should have a passed dummy argument class(a)
! and some other argument for the generic resolution
! for example:
subroutine valua1(self, x)
class(a), intent(in) :: self
real, intent(inout) :: x
end subroutine
subroutine valua2(self, x)
class(a), intent(in) :: self
integer, intent(inout) :: x
end subroutine
end interface
This way you cannot create variables of type(a)
, but you can make extended types which implement their own versions of value
.
这篇关于fortran类声明的虚拟参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!