wangmianzhi 发表于 2011-12-1 22:30:15

关于fortran里面deferred binding的问题

不理解为什么需要deferred binding这个功能。
既然用最简单的带指针的type-bounded procedure就可以实现统一接口不同实现(如下),为什么还要写成deferred binding这种形式呢?
type a
contains
   procedure::p=>pa
end type

type b
contains
    procedure::p=>pb
end type
多谢指点。

wangmianzhi 发表于 2011-12-1 23:21:33

本帖最后由 wangmianzhi 于 2011-12-1 23:35 编辑

这可能是一个目的:
如果编译器不知道对象具体的类,比如使用指向抽象类的指针访问具体对象。就需要使用延时绑定来确定具体的实现。
一个例子:module a

type,abstract::ab
    integer::i
contains
    procedure(pab),deferred::p
end type

abstract interface
    subroutine pab(this)
      import ab
      class(ab)::this
    end subroutine
end interface

type,extends(ab)::e
    class(ab),pointer::next
contains
    procedure::p=>pe
end type

contains

subroutine pe(this)
    class(e)::this
    write(*,*),this%i
end subroutine

end module

program test
use a
type(e),target::dd
dd%i=33
dd%next=>dd
call dd%next%p()
end program

qinxl 发表于 2011-12-4 20:45:37

真牛。在需要面向对象时,我一般让会一脚踢开Fortran。
页: [1]
查看完整版本: 关于fortran里面deferred binding的问题