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

Extends of derived type and access for its instance variable

oleglebedev
New Contributor I
309 Views
Good day,
I have coded a simple test
[fortran]MODULE foo_1_class
    IMPLICIT NONE
!
PRIVATE
!
TYPE, public :: foo_1
  PRIVATE
  integer :: xx = 1
    CONTAINS
    PRIVATE
      procedure, public :: getX
END TYPE foo_1
!
    CONTAINS
!
ELEMENTAL integer function getX(this) RESULT(res)
    implicit none
    class(foo_1), intent(IN) :: this
res = this%xx
return
end function getX
!
!
!
END MODULE foo_1_class



MODULE foo_2_class
    USE foo_1_class
    IMPLICIT NONE
!
PRIVATE
!
TYPE, public, extends(foo_1) :: foo_2
  PRIVATE
  integer :: yy = 3
    CONTAINS
    PRIVATE
      procedure, public :: calc
END TYPE foo_2
!
CONTAINS
!
ELEMENTAL integer function calc(this) RESULT(res)
    implicit none
    class(foo_2), intent(IN) :: this
res = this%getX() + this%yy
    !res = this%xx + this%yy ! Why could I not type this way?
    !!! error #6292: The parent type of this field is use associated with the PRIVATE fields attribute   [XX]
return
end function calc
!
!
END MODULE foo_2_class


PROGRAM main
    USE foo_2_class
    IMPLICIT NONE
    type(foo_2) :: foo
!
write( *, * ) 'res is ', foo%calc()

END PROGRAM main[/fortran]
I don`t understand. The all instance variables of foo_1 areinherited by foo_2. I suposed that an access to these ones should be possible through the foo_2_class module (with help of the class key-word). But I got the error message.
I compiled by ifortVersion 12.0.3.174 Build 20110309 withFC := ifort -g -traceback -check all -openmp -std03
Oleg.
0 Kudos
2 Replies
Steven_L_Intel1
Employee
309 Views
As the compiler notes, you have made component xx PRIVATE, and therefore it is not accessible outside the module where it is declared. The ability to restrict access to individual components is important in structured programming, hence the use of get functions. You can access yy because it is defined in the same module as the reference. Type extension does not cause accessibility to be ignored.
0 Kudos
oleglebedev
New Contributor I
309 Views
I thought so and I decided to check it up.
Oleg
0 Kudos
Reply