fortran90 - Store a "pointer to function" in fortran 90? -


in fortran, can pass function/subroutine argument function/subroutine b, can store later retrieval , use?

for example, allowed in c

int foo(float, char, char) { /*whatever*/};  int (*pointertofunction)(float, char, char); pointertofunction = foo; 

in fortran can pass subroutine argument

subroutine foo ! whatever end subroutine foo  subroutine bar(func)     call func end subroutine bar  program x      call bar(foo)  end program 

but how can store address of foo in similar way c ?

starting so-called "fortran 2003" (iso/iec 1539-2004) procedure pointers part of fortran language. it's of major new features of fortran language.

usage example fortran wiki.


stefano, mentioned strategy design pattern. in fortran 2003 can use pure oop way implement (without procedure pointers). offhand example:

strategies.f90

module strategies    implicit none    private    public :: strategies_transportation_strategy, &             strategies_by_taxi_strategy, &             strategies_by_bus_strategy    type, abstract :: strategies_transportation_strategy   contains     procedure(transportation_strategy_go), deferred :: go   end type strategies_transportation_strategy    type, extends(strategies_transportation_strategy) :: strategies_by_taxi_strategy   contains     procedure :: go => strategies_by_taxi_strategy_go   end type strategies_by_taxi_strategy    type, extends(strategies_transportation_strategy) :: strategies_by_bus_strategy   contains     procedure :: go => strategies_by_bus_strategy_go   end type strategies_by_bus_strategy    abstract interface     subroutine transportation_strategy_go(this)       import strategies_transportation_strategy       class(strategies_transportation_strategy), intent(in) ::     end subroutine transportation_strategy_go   end interface    contains      subroutine strategies_by_taxi_strategy_go(this)       class(strategies_by_taxi_strategy), intent(in) ::        print *, "we using taxi."      end subroutine strategies_by_taxi_strategy_go      subroutine strategies_by_bus_strategy_go(this)       class(strategies_by_bus_strategy), intent(in) ::        print *, "we using public transport."      end subroutine strategies_by_bus_strategy_go  end module strategies 

vehicles.f90

module vehicles    use strategies    implicit none    private    public :: vehicles_vehicle, &             vehicles_taxi, &             vehicles_bus    type, abstract :: vehicles_vehicle     private     class(strategies_transportation_strategy), allocatable :: transportation_strategy   contains     procedure :: set_transportation_strategy => vehicle_set_transportation_strategy     procedure :: go => vehicle_go   end type vehicles_vehicle    type, extends(vehicles_vehicle) :: vehicles_taxi   contains     procedure :: init => taxi_init   end type vehicles_taxi    type, extends(vehicles_vehicle) :: vehicles_bus   contains     procedure :: init => bus_init   end type vehicles_bus    contains      subroutine vehicle_go(this)       class(vehicles_vehicle), intent(in) ::        call this%transportation_strategy%go()      end subroutine vehicle_go      subroutine vehicle_set_transportation_strategy(this, new_transportation_strategy)       class(vehicles_vehicle), intent(inout) ::       class(strategies_transportation_strategy), intent(in) :: new_transportation_strategy        if (allocated(this%transportation_strategy))         deallocate (this%transportation_strategy)       end if        allocate (this%transportation_strategy, source=new_transportation_strategy)      end subroutine vehicle_set_transportation_strategy      subroutine taxi_init(this)       class(vehicles_taxi), intent(out) ::        type(strategies_by_taxi_strategy) :: by_taxi_strategy        call this%set_transportation_strategy(by_taxi_strategy)      end subroutine taxi_init      subroutine bus_init(this)       class(vehicles_bus), intent(out) ::        type(strategies_by_bus_strategy) :: by_bus_strategy        call this%set_transportation_strategy(by_bus_strategy)      end subroutine bus_init  end module vehicles 

main.f90

program main    use vehicles    implicit none    type(vehicles_taxi) :: taxi   type(vehicles_bus) :: bus    call taxi%init()   call bus%init()    call taxi%go()   call bus%go()  end program main 

at least works using gfortran 4.6 (20100925).


Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -