Overloaded functions returning pointers to a base

2019-06-14 09:54发布

问题:

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...

回答1:

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