【发布时间】:2013-05-07 21:05:37
【问题描述】:
我想我可以轻松在这里使用一些帮助,因为我在搞乱一些 fortran 2003 但似乎无法真正理解如何做事。 事实是我需要编写一个 fortran 代码,在模块内声明一个新的数据类型 它的成员之一是指向实际函数的指针。类似的东西
module new_mod
type my_type
real*8 :: a, b
(here something that declares a 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
然后在主程序中我想要类似的东西
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
同时
所以主要思想是 module_new 包含一个类型,该类型具有指向真实的指针 功能。新类型的对象中的这个指针必须能够指向主程序中的不同函数。 我已经看到可以用抽象接口等做类似的事情,但老实说,我在这里一团糟。如果有人可以提供帮助,我将不胜感激。 干杯...
【问题讨论】:
标签: function pointers types fortran