重载函数返回指向基类型的指针以及fortran 2003中的抽象接口? [英] Overloaded functions returning pointers to a base type alongside an abstract interface in fortran 2003?
问题描述
我正在Fortran2003中编写一个链表结构。这个链表中的节点有两个交替的变体。该列表表示边缘的拓扑环,每条边被两个顶点限定,并且每个顶点连接到两条边。
这些结构被声明为抽象的NODE_T类型,并由两个子类VN_T和EN_T实现。 VN_T指向两个EN_T,反之亦然。
由于列表在类型之间交替,所以指针存储在子类中。
我的问题是向VN_T和EN_T添加类型绑定函数(方法?),这些函数被称为NEXT()和PREV(),并且通过多态性做正确的事情。 VN_T%NEXT()应跳过两个步骤,并获得下一个VN_T(与EN_T分隔开始的一个)。
我的类型/类定义如下:
TYPE,ABSTRACT :: NODE_T
INTEGER :: IENT
INTEGER :: ISIDE
INTEGER :: ISUBTYPE
INTEGER :: IPAD
CONTAINS
PROCEDURE(NEXTA),DEFERRED :: NEXT
PROCEDURE(PREVA),DEFERRED :: PREV
END TYPE NODE_T
抽象接口
函数NEXTA(PENT)
IMPORT NODE_T
CLASS(NODE_T),POINTER :: NEXTA
CLASS(NODE_T):: PENT
END FUNCTION
功能PREVA(PENT)
导入NODE_T
CLASS(NODE_T),POINTER :: PREVA
CLASS(NODE_T):: PENT
END FUNCTION
END INTERFACE
TYPE,EXTENDS(NODE_T):: VN_T
DOUBLE PRECISION DT
CLASS( EN_T),POINTER :: N
CLASS(EN_T),POINTER :: P
CONTAINS
PROCEDURE :: NEXT => NEXTV
PROCEDURE :: PREV => PREVV
END TYPE
TYPE,EXTENDS(NODE_T):: EN_T
CLASS(VN_T),POINTER :: N
CLASS(VN_T),POINTER :: P
CONTAINS
PROCEDURE :: NEXT => NEXTE
PROCEDURE :: PREV => PREVE
END TYPE
next / prev例程的实现大致相同, next / prev和EN / VN的四种组合:
功能NEXTE(PENT)
CLASS(NODE_T), POINTER :: NEXTE
CLASS(EN_T)PENT
NEXTE => PENT%N%N
END FUNCTION
当我调用这些函数时:
CLASS(NODE_T),POINTER :: NODE1,NODE2
NODE2 => NODE1%NEXT()
然后我的编译器(Intel FORTRAN XE Composer 2011,即v12.0)如果最右边的部分名称是抽象类型,data-ref应该是多态的[NEXT ]
NODE2 => NODE1%NEXT()
--------------------------- ^
基于此此文档似乎表明它正试图调用基类上的procdure NEXT,而不是选择其中一个子类实现它应该能够根据NODE1的具体类型在右侧进行操作,对吧?)。
我是OOP的新特性F2003,所以我在JAVA中对这种模式做了一个模型,这让我很满意。任何人都可以阐明这是否是a)F2003的OOP行为差异,2)编译器bug或3)我只是braindead ...
您可能需要更新您的编译器。正如本文讨论的那样
http://software.intel.com/zh-cn/forums/showthread.php?t = 78855
Fortran标准最初不允许直接调用抽象类型的类型绑定过程,您正在尝试去做。然而,这个标准已经改变了,它现在应该已经进入英特尔Fortran。
另一种方法是修改基类为:
TYPE :: NODE_T
INTEGER :: IENT
INTEGER :: ISIDE
INTEGER :: ISUBTYPE
INTEGER :: IPAD
CONTAINS
PROCEDURE :: NEXT
PROCEDURE :: PREV
END TYPE NODE_T
I'm writing a linked list structure in Fortran2003. The nodes in this linked list come in two varieties that alternate. The list represents a topological ring of edges, each edge is bounded by two vertices, and each vertex connects to two edges. These structures are declared as an abstract NODE_T type, and implemented by two subclasses, VN_T and EN_T. A VN_T points to two EN_Ts, and vice versa. Since the list alternates between types, the pointers are stored in the subclasses. My problem is with adding type bound functions (methods?) to VN_T and EN_T that are called NEXT() and PREV(), and that do the right thing via polymorphism. VN_T%NEXT() should skip on two steps, and get the next VN_T (that is separated from the starting one by an EN_T).
My type/class definitions are as follows:
TYPE, ABSTRACT :: NODE_T
INTEGER :: IENT
INTEGER :: ISIDE
INTEGER :: ISUBTYPE
INTEGER :: IPAD
CONTAINS
PROCEDURE(NEXTA), DEFERRED :: NEXT
PROCEDURE(PREVA), DEFERRED :: PREV
END TYPE NODE_T
ABSTRACT INTERFACE
FUNCTION NEXTA(PENT)
IMPORT NODE_T
CLASS(NODE_T), POINTER :: NEXTA
CLASS(NODE_T) :: PENT
END FUNCTION
FUNCTION PREVA(PENT)
IMPORT NODE_T
CLASS(NODE_T), POINTER :: PREVA
CLASS(NODE_T) :: PENT
END FUNCTION
END INTERFACE
TYPE, EXTENDS(NODE_T) :: VN_T
DOUBLE PRECISION DT
CLASS(EN_T), POINTER :: N
CLASS(EN_T), POINTER :: P
CONTAINS
PROCEDURE :: NEXT => NEXTV
PROCEDURE :: PREV => PREVV
END TYPE
TYPE, EXTENDS(NODE_T) :: EN_T
CLASS(VN_T), POINTER :: N
CLASS(VN_T), POINTER :: P
CONTAINS
PROCEDURE :: NEXT => NEXTE
PROCEDURE :: PREV => PREVE
END TYPE
The implementations of the next/prev routines are all much the same, the four combinations of next/prev and EN/VN:
FUNCTION NEXTE(PENT)
CLASS(NODE_T), POINTER :: NEXTE
CLASS(EN_T) PENT
NEXTE => PENT%N%N
END FUNCTION
When I call these functions:
CLASS(NODE_T), POINTER :: NODE1, NODE2
NODE2 => NODE1%NEXT()
Then my compiler (Intel FORTRAN XE Composer 2011, i.e. v12.0) complains with the message
error #8314: If the rightmost part-name is of abstract type, data-ref shall be polymorphic [NEXT]
NODE2 => NODE1%NEXT()
---------------------------^
Which based on this this documentation seems to indicate that it is trying to call the procdure NEXT on the base class rather than selecting one of the subclass implementations (which it should be able to do based on the concrete type of NODE1 which is on the right hand side, right?).
I'm new at the OOP features of F2003, so I made a mockup of this pattern in JAVA which I got working to my satisfaction. Can anyone shed light on whether this is either a) a difference in the OOP behaviour of F2003, 2) a compiler bug or 3) just me being braindead...
You may need to update your compiler. As discussed in this thread
http://software.intel.com/en-us/forums/showthread.php?t=78855
the Fortran standard originally disallowed calling type-bound procedures of abstract types directly, which you are trying to do. However, the standard was changed and it should have made it into Intel Fortran by now.
An alternative is to modify the base class as:
TYPE :: NODE_T
INTEGER :: IENT
INTEGER :: ISIDE
INTEGER :: ISUBTYPE
INTEGER :: IPAD
CONTAINS
PROCEDURE :: NEXT
PROCEDURE :: PREV
END TYPE NODE_T
这篇关于重载函数返回指向基类型的指针以及fortran 2003中的抽象接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!