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?

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 - while you are assigning a procedure pointer rather than doing argument association, similar rules come into play. Commented Jan 5, 2015 at 12:37

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

How do you set the value of funcPtr?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.