Quantcast
Channel: Intel® Fortran Compiler for Linux* and macOS*
Viewing all articles
Browse latest Browse all 2583

Polymorphic pointer to parent class not working

$
0
0

Consider the following class structure, which involves three separate modules:

!----------------------- in file a.f

      module parent_body_mod
      type :: face
         class(parent_body), pointer :: bPtr
      end type
      type, abstract :: parent_body
         integer i
         type(face) :: f
      end type
      end module parent_body_mod

!------------------------ in file b.f

      module body_mod
      use parent_body_mod

      type, extends(parent_body) :: body
      end type

      interface body
         procedure :: new_body
      end interface

      contains

      function new_body() result(b) 
      type(body), target :: b
      b%i = 123
      b%f%bPtr => b
      end function
      end module body_mod

!--------------------------- in file c.f

      module body_group_mod
      use body_mod
      type :: body_group
         type(body), allocatable :: b
      end type
      interface body_group
         procedure :: new_body_group
      end interface
      contains 
      function new_body_group() result(bg)
      type(body_group) :: bg

      allocate(bg%b)
      bg%b = body()

      end function
      end module body_group_mod

!------------------- The main program

      use body_group_mod

      type(body_group) :: my_bg

      my_bg = body_group()      

      print *, my_bg%b%f%bPtr%i

      end

!--------------------------------------

The expected output is 123, whereas the actual output is something random. The code is compiled using ifort version 18.0.1. Note that the same issue doesn't happen when using "body" class itself, i.e. the following works just fine:

type(body), allocatable :: my_b

allocate(my_b)

my_b = body()

print *, my_b%f%bPtr%i     ! This produces 123 as expected.

Any help is appreciated.


Viewing all articles
Browse latest Browse all 2583

Trending Articles