本帖最后由 weixing1531 于 2019-7-6 22:08 编辑
楼主的设想还是可以实现的
只是class(*)多态指针p的断开引用必须在select type内部才行
但Fortran2018标准已经放松该限制
摘自电子书《Modern Fortran Explained Incorporating Fortran 2018》
下载网址:http://www.doc88.com/p-0601727992809.html
[Fortran] 纯文本查看 复制代码 program main
implicit none
integer,target::a=3
real,target::b=8.15
class(*),pointer::p
ALLOCATE(p,source=a) !f2018标准支持 p=a
call write_p(p)
p=>b
call write_p(p)
ALLOCATE(p,source=b) !f2018标准支持 p=b
call write_p(p)
p=>a
call write_p(p)
contains
subroutine write_p(p)
class(*),pointer,intent(in)::p
select type(p)
type is(integer)
write(*,"(I3)")p
type is(real)
write(*,"(F8.2)")p
end select
end subroutine write_p
end program
|