5 ! Contributed by Reinhold Bader.
7 ! Before TYPE(ext)'s .tr. wrongly called the base type's trace
8 ! instead of ext's trace_ext.
13 integer, public
:: base_cnt
= 0
16 real :: r(2,2) = reshape( (/ 1.0, 2.0, 3.0, 4.0 /), (/ 2, 2 /))
18 procedure
, private
:: trace
19 generic
:: operator(.tr
.) => trace
22 complex function trace(this
)
23 class(base
), intent(in
) :: this
24 base_cnt
= base_cnt
+ 1
25 ! write(*,*) 'executing base'
26 trace
= this
%r(1,1) + this
%r(2,2)
34 integer, public
:: ext_cnt
= 0
35 public
:: base
, base_cnt
36 type, public
, extends(base
) :: ext
38 real :: i(2,2) = reshape( (/ 1.0, 1.0, 1.0, 1.5 /), (/ 2, 2 /))
40 procedure
, private
:: trace
=> trace_ext
43 complex function trace_ext(this
)
44 class(ext
), intent(in
) :: this
46 ! the following should be executed through invoking .tr. p below
47 ! write(*,*) 'executing override'
49 trace_ext
= .tr
. this
%base
+ (0.0, 1.0) * ( this
%i(1,1) + this
%i(2,2) )
50 end function trace_ext
60 ! Note: ext's ".tr." (trace_ext) calls also base's "trace"
64 if (base_cnt
/= 0 .or
. ext_cnt
/= 0) STOP 1
66 if (base_cnt
/= 1 .or
. ext_cnt
/= 0) STOP 2
68 if (base_cnt
/= 2 .or
. ext_cnt
/= 1) STOP 3
70 if (abs(.tr
. o
- 5.0 ) < 1.0e-6 .and
. abs( .tr
. p
- (5.0,2.5)) < 1.0e-6) &
72 if (base_cnt
/= 4 .or
. ext_cnt
/= 2) STOP 4
78 end program test_override