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

How to use extended type as parameter of an external procedure?

lauhwa
Novice
221 Views

Holle,

As in followed codes, the first parameter of the external function "printi" is the type of "c_t", while, i want to pass the object of its extended type. Is there some way to implement?

 

module dftype

type:: c_t
integer::j=0
!contains
!procedure::printi
end type
type,abstract,extends(c_t):: cc_t
integer::i=0
!contains
!procedure::printi
end type
contains

type(cc_t) function alloccc()
i = 0
end function
subroutine printi(this,i)
class(cc_t)::this
write(*,*)this%i+ i
end subroutine
subroutine printa(a,i)
!class(cc_t)::this
write(*,*) a+i

end subroutine

subroutine prfun(this,fun,fun2)
interface
subroutine fun(this,i)
import c_t
class(c_t)::this
end
end interface

class(c_t)::this
external::fun2

call fun2(1e0,1)
call fun(this,1)

end subroutine

end module

program main
use dftype
class(cc_t),allocatable::cc1
cc1 = alloccc()
cc1%i=1
call prfun(cc1,printi,printa)
end

 

 

0 Kudos
2 Replies
lauhwa
Novice
211 Views

This is the right code

module dftype

type:: c_t
integer::j=0
!contains
!procedure::printi
end type
type,abstract,extends(c_t):: cc_t
integer::i=0
!contains
!procedure::printi
end type
contains

type(cc_t) function alloccc()
i = 0
end function
subroutine printi(this,i)
class(c_t)::this
write(*,*)this%j+ i
end subroutine
subroutine printa(a,i)
!class(cc_t)::this
write(*,*) a+i

end subroutine

subroutine prfun(this,fun,fun2)
interface
subroutine fun(this,i)
import c_t
class(c_t)::this
end
end interface

class(c_t)::this
external::fun2

call fun2(1e0,1)
call fun(this,1)

end subroutine

end module

program main
use dftype
class(cc_t),allocatable::cc1
cc1 = alloccc()
cc1%i=1
call prfun(cc1,printi,printa)
end

 

 

0 Kudos
andrew_4619
Honored Contributor III
188 Views

Use a Fortran markup box to make it readable.

module dftype

type:: c_t
integer::j=0
!contains
!procedure::printi
end type
type,abstract,extends(c_t):: cc_t
integer::i=0
!contains
!procedure::printi
end type
contains

type(cc_t) function alloccc()
i = 0
end function
subroutine printi(this,i)
class(c_t)::this
write(*,*)this%j+ i
end subroutine
subroutine printa(a,i)
!class(cc_t)::this
write(*,*) a+i

end subroutine

subroutine prfun(this,fun,fun2)
interface
subroutine fun(this,i)
import c_t
class(c_t)::this
end
end interface

class(c_t)::this
external::fun2

call fun2(1e0,1)
call fun(this,1)

end subroutine

end module

program main
use dftype
class(cc_t),allocatable::cc1
cc1 = alloccc()
cc1%i=1
call prfun(cc1,printi,printa)
end

 

 

0 Kudos
Reply