- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
Link Copied
2 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I thought so and I decided to check it up.
Oleg
Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page