[fpc-pascal] Surprise. Comparison of method is only done on the routine address
Dennis
dec12 at avidsoft.com.hk
Tue Jul 12 08:11:29 CEST 2016
I always thought comparison of methods is done on both the data and the
code part.
TMethod = record
Code : CodePointer;
Data : Pointer;
end;
But the following proves it is NOT.
What is the rationale behind such behavior?
--------------
program compare_events;
{$mode objfpc}{$H+}
uses classes;
type
TA=class
procedure Proc(aSender : TObject);virtual;
end;
TB=class(TA)
procedure Proc(aSender : TObject);override;
end;
TC=Class(TA)
end;
var
E1,E2,E3 : TNotifyEvent;
A : TA;
B : TB;
C : TC;
procedure TB.Proc(aSender: TObject);
begin
inherited Proc(aSender);
end;
procedure TA.Proc(aSender: TObject);
begin
end;
begin
a := TA.Create;
B := TB.Create;
C := TC.Create;
try
E1 := @(A.Proc);
E2 := @(B.Proc);
E3 := @(C.Proc);
if E1 = E2 then
Writeln('A.Proc = B.Proc')
else
Writeln('A.Proc <> B.Proc');
if E1 = E3 then
Writeln('A.Proc = C.Proc')
else
Writeln('A.Proc <> C.Proc') ;
Readln;
finally
A.Free;
B.Free;
C.Free;
end;
end.
{===============the program gives the following output}
A.Proc <> B.Proc
A.Proc = C.Proc
{end of output}
More information about the fpc-pascal
mailing list