Problem with procedure pointers

Arjen Markus arjen.markus895@gmail.com
Thu Mar 10 11:49:00 GMT 2011


Hi,

I have the following program:

! proc_pointer.f90 --
!     Check strange behaviour with procedure pointers
!
module proc_pointers

    implicit none

    !
    ! Rectangle
    !
    type :: rectangle
        real :: width, height
        procedure(get_area), pointer, pass(this) :: get_special_area
=> get_my_area
        procedure(get_area), pointer, nopass     ::
get_second_special_area => get_my_area
    end type rectangle

    abstract interface
        real function get_area( this )
            import                       :: rectangle
            class(rectangle), intent(in) :: this
        end function get_area
    end interface

contains

!
! The various routines and functions we need
!
real function get_my_area( this )
    type(rectangle), intent(in) :: this

    write(*,*) 'This: ', this%width, this%height

    get_my_area = 3.0 * this%width * this%height

end function get_my_area

end module proc_pointers

!
! Small test program
program test_objects

    use proc_pointers

    implicit none

    type(rectangle), target   :: rect
    real                       :: area

    rect%width  = 1.0
    rect%height = 2.0

    write(*,*) rect

    area = rect%get_special_area()
    write(*,*) 'Special area:', area

    area = rect%get_second_special_area(rect)
    write(*,*) 'Second special area:', area

contains

end program test_objects


And where I would expect the computed area to be 6, I get zero. Hence
the write statements.
Here is the output of the program:

   1.0000000       2.0000000      5.88534145E-39  5.88534145E-39
 This:   6.14154925E-39  6.14152123E-39
 Special area:   0.0000000
 This:   6.14154925E-39  6.14152123E-39
 Second special area:   0.0000000

(My guess is that the two strange numbers at the end of the first line
are procedure pointers)

As you can see in the output, the argument that gets passed bares very
little resemblance
to the rectangle object I have defined.

Can anyone shed some light on this?

The version of gfortran I am using is:

Built by Equation Solution <http://www.Equation.com>.
Using built-in specs.
COLLECT_GCC=gfortran
COLLECT_LTO_WRAPPER=d:/gcc4.6/bin/../libexec/gcc/i686-pc-mingw32/4.6.0/lto-wrapper.exe
Target: i686-pc-mingw32
Thread model: win32
gcc version 4.6.0 20110101 (experimental) (GCC)

Regards,

Arjen



More information about the Fortran mailing list