Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28523 Discussions

Pointer to a parent of a class fails to produce an appropriate result

pwl_b
Beginner
192 Views
The following code compiles with ifort12.1.0 20110811 and gfortran4.6.1 20110422 without any warnings. Yet the results produced by programs compiled with respective compilers differ.
[fortran]module class_a
  type :: a
     real, pointer :: t => null()
     real :: p = 11.
  end type a
end module class_a

module class_a_extended
  use class_a
  type, extends(a) :: a_extended
  end type a_extended
end module class_a_extended

module class_b
  use class_a
  type :: b
     class(a), pointer :: ap
  end type b
end module class_b

program inheritance_test
  use class_a_extended
  use class_b

  type(a_extended), target :: a_extended_type
  class(b), pointer :: b_class
  type(b) :: b_type

  allocate(a_extended_type % t)
  allocate(b_class)

  a_extended_type % t = 888.

  b_class % ap => a_extended_type % a
  b_type % ap => a_extended_type % a

  ! this does not work
  print *, a_extended_type % t
  print *, b_class % ap % t
  print *, b_type % ap % t

  ! this works just fine
  print *, a_extended_type % p
  print *, b_class % ap % p
  print *, b_type % ap % p

end program inheritance_test
[/fortran]
gfortran compiled code yields:
888.00000
888.00000
888.00000
11.000000
11.000000
11.000000
while ifort compiled code prints:
888.0000
1.8362133E-33
888.0000
11.00000
11.00000
11.00000
0 Kudos
2 Replies
pwl_b
Beginner
192 Views
It starts to work with ifort when
[fortran]class(a), pointer :: ap[/fortran]
is changed to
[fortran]type(a), pointer :: ap  [/fortran]
in a definition of type b
0 Kudos
Steven_L_Intel1
Employee
192 Views
Thanks - we'll take a look.
0 Kudos
Reply