I’ve encountered an interesting problem while overriding virtual class method. Consider the next program:
program Project1; {$APPTYPE CONSOLE} uses SysUtils; type TBase = class class function GetName: string; virtual; class procedure Test; static; end; TChild = class(TBase) class function GetName: string; override; end; class function TBase.GetName: string; begin Result:= 'TBase'; end; class procedure TBase.Test; begin Writeln(GetName); end; class function TChild.GetName: string; begin Result:= 'TChild'; end; begin TBase.Test; TChild.Test; Readln; end.
Despite the GetName function is overridden in TChild class the output is
The bug is static specifier of the TBase.Test method. After the specifier is removed the output is as expected:
The reason of the above compiler behavior is clear – with static specifier in TBase.Test method declaration the compiler (Delphi XE was used) receives no class information and statically binds TBase.GetName class method. The bad thing is that the compiler is silent about the problem and issues no warning.