Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
New Contributor I
8 Views

Calling parent procedures

Is it possible to call a parent procedure in a user-defined derived type?

module my_mod
    implicit none
    type :: parent_t
        private
        integer :: a = 0
    contains
        procedure, public :: print => print_pt
    end type
    
    type, extends(parent_t) :: my_t
    contains
        procedure, public :: print => print_mt
    end type my_t
    
contains
    
    subroutine print_pt(this)
        class(parent_t), intent(in) :: this
        write(*,'(i0)') this%a
    end subroutine print_pt
    
    subroutine print_mt(this)
        class(my_t), intent(in) :: this
        write(*,'(a)') 'My value is'
        ! How do I call print_pt here?
        !! call this%print_pt() gives compilation error 6460
    end subroutine print_mt

end module my_mod

Hopefully the above code is sufficiently explanatory. I have tried a direct call to print_pt but that gives a compiler error

error #6460: This is not a field name that is defined in the encompassing structure.   [PRINT_PT]

What is the correct syntax? I'm using Version 16.0.3.207. Thanks.

0 Kudos
2 Replies
Highlighted
Valued Contributor II
8 Views

Definitely:

call this%parent_t%print()

The method name is print - if you want to invoke the underlying routine directly, that is possible too:

call print_mt(this)

or:

call print_pt(this)

 

0 Kudos
Highlighted
New Contributor I
8 Views

Excellent. call this%parent_t%print() works perfectly. Thanks.

0 Kudos