[fpc-devel] Static class methods can or cannot be virtual?

Denis Kozlov dezlov at gmail.com
Mon Jan 22 18:00:18 CET 2018


Hello,

Can static class methods be virtual?

According to the FPC documentation, static class methods cannot be 
virtual, as stated here:
https://www.freepascal.org/docs-html/ref/refsu30.html

But in practice, FPC compiles and executes virtual and overridden static 
class methods just fine.

The following code prints "AB" with FPC 3.0.4 and 2.6.4.

=======================================
type
   TTestA = class
   protected
     class function GetName: String; virtual; static;
   public
     class property Name: String read GetName;
   end;

type
   TTestB = class(TTestA)
   protected
     class function GetName: String; override; static;
   end;

class function TTestA.GetName: String;
begin
   Result := 'A';
end;

class function TTestB.GetName: String;
begin
   Result := 'B';
end;

begin
   WriteLn(TTestA.Name, TTestB.Name);
end.
=======================================

Thanks,
Denis



More information about the fpc-devel mailing list