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

Calling parent procedures

Simon_Geard
New Contributor I
277 Views

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
Arjen_Markus
Honored Contributor I
277 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
Simon_Geard
New Contributor I
277 Views

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

0 Kudos
Reply