Pointer to a function inside a derived type on a module in fortran -


i guess easily use here, since i'm messing around fortran 2003 can't seem understand how things really. fact need write fortran code declares, inside module, new data type has 1 of members pointer real function. like

module new_mod   type my_type     real*8 :: a, b     (here declares real*8 function), pointer :: ptr   end type my_type end module_new  module funcs   real*8 function function1(x)     real*8 :: x     function1 = x*x   end function function1   real*8 function function2(x)     real*8 :: x     function2 = x*x   end function function2 end module funcs 

then in main program have like

program my_prog   use module_new   use module_funcs   implicit none   real*8 :: y, z   type(my_type) :: atom   ...   atom%ptr => function1   y = atom%ptr(x)   ...   atom%ptr => function2   z = atom%ptr(x) end program my_prog 

while

so main idea module_new contains type has pointer real function. pointer in th eobjects of new type must able point different functions in main program. have seen 1 can similar things abstract interfaces , such, honestly, i'm in mess here. if help, i'll appreciate that. cheers...

well, not type of question send stackoverflow, code needs "slight improvement" (by appropriate definition of slight) work:

module accuracy   implicit none    integer, parameter :: dp = kind(1.0d0)  end module accuracy   module typedef   use accuracy   implicit none    type :: mytype     real(dp) :: aa, bb     procedure(myinterface), pointer, nopass :: myfunc   end type mytype    abstract interface     function myinterface(xx)       import :: dp       real(dp), intent(in) :: xx       real(dp) :: myinterface     end function myinterface   end interface  end module typedef  module funcs   use accuracy   implicit none  contains    function func1(xx)     real(dp), intent(in) :: xx     real(dp) :: func1      func1 = xx    end function func1    function func2(xx)     real(dp), intent(in) :: xx     real(dp) :: func2      func2 = 2.0_dp * xx    end function func2  end module funcs   program test   use accuracy   use typedef   use funcs   implicit none    real(dp) :: xx   type(mytype) :: atom    xx = 12.0_dp   atom%myfunc => func1   print *, atom%myfunc(xx)   atom%myfunc => func2   print *, atom%myfunc(xx)  end program test 

there several things worth mentioned:

  • you should use 1 global parameter accuracy (see module accuracy) , forget real*8.

  • your procedure pointer in derived type needs interface, provided within following abstract interface block (see 'abstract interfaces' in f2003 book).

  • you need nopass option procedure pointer in derived type otherwise fortran assume first parameter passed function/subroutine derived type (see 'type bound procedures' in f2003 book).

  • finally, although rather obvious: should read book fortran 2003 features if serious using them in production code.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -