2

I have some problems with old and new Fortran code. The new part is object-oriented, the old part works with function pointers.

My problem is, that I want to a assign a member function to a function pointer, so that the function works with this special object. Here is an example code with the same error:

module test

   ! abstract class
   type, abstract :: base      
   contains
      procedure (doSth), deferred :: doSomething
   end type base

   ! deferred function
   abstract interface
      subroutine doSth(this)
      import :: base    
         class(base)  :: this
      end subroutine      
   end interface


   ! derived class
   type, extends(base) :: child      
   contains
      procedure :: doSomething => do_Sth
   end type child

   contains
   ! deferred function implemented by child   
   subroutine do_Sth(this)
      class(child)  :: this
      ! ...
      ! ...
   end subroutine   


   ! function pointer to member function
   subroutine get_functionPointer()
      procedure() , pointer :: funcPtr
      type (child), pointer :: childPtr

      allocate (childPtr)
      funcPtr => childPtr%doSomething
      ! ... This does not work      
   end subroutine
end module 

This gives me the error message:

error #8191: The procedure target must be a procedure or a procedure pointer.

Is there any possibility to overcome this problem?

Benjamin
  • 21
  • 1
  • 2
    Fortran does not have the concept of member functions - the thing that you are trying to pointer assign to funcPtr is a binding. The name of the relevant procedure for the right hand side is `do_Sth`. Perhaps read the accepted answer to [this](http://stackoverflow.com/questions/11318816/passing-type-bound-procedures-as-arguments-in-fortran-90) - while you are assigning a procedure pointer rather than doing argument association, similar rules come into play. – IanH Jan 05 '15 at 12:37

1 Answers1

0

With some help, this seems to work:

             :
             :
  ! function pointer to member function
   subroutine get_functionPointer()
      procedure(doSth), pointer :: funcPtr
      type (child), pointer :: childPtr

      allocate (childPtr)
      call funcPtr(childPtr)
   end subroutine
            :
            :

Thanks a lot for your help.

Benjamin
  • 21
  • 1