Recent

Author Topic: [SOLVED] Static Class Procedure cannot be virtual..  (Read 2242 times)

darksky666

  • New Member
  • *
  • Posts: 38
[SOLVED] Static Class Procedure cannot be virtual..
« on: July 19, 2018, 12:57:15 pm »
I wanted to know what error it would give but it worked!?

Code: Pascal  [Select][+][-]
  1.   TClass1 = class
  2.     class procedure classProcStatic; virtual; static;
  3.   end;
  4.  
  5.  
  6.   { TClass2 }
  7.  
  8.   TClass2 = class (TClass1)
  9.     class procedure classProcStatic;  override; static;
  10.   end;
  11.  
  12. { TClass2 }
  13.  
  14. class procedure TClass2.classProcStatic;
  15. begin
  16.   ShowMessage('Class 2 Static Procedure');
  17. //  inherited;
  18. end;
  19.  
  20. { TClass1 }
  21.  
  22. class procedure TClass1.classProcStatic;
  23. begin
  24.   ShowMessage('Class 1 Static Procedure');
  25. end;
  26.  
  27. procedure TForm1.FormCreate(Sender: TObject);
  28. var
  29.   c1 : TClass1;
  30. begin
  31.   c1 := TClass2.Create;
  32.  
  33.   c1.classProcStatic;
  34.  
  35.   c1.Free;
  36. end;
  37.  

Thanks

EDIT: I know it's a sh!tpost, but why is it working? I was expecting the compiler to stop and produce error.. placing static before virtual/override didn't change anything..
« Last Edit: July 19, 2018, 02:41:59 pm by darksky666 »

Thaddy

  • Hero Member
  • *****
  • Posts: 14201
  • Probably until I exterminate Putin.
« Last Edit: July 19, 2018, 01:09:50 pm by Thaddy »
Specialize a type, not a var.

darksky666

  • New Member
  • *
  • Posts: 38
Re: Static Class Procedure cannot be virtual..
« Reply #2 on: July 19, 2018, 01:14:45 pm »
Thanks Thaddy, but .. (now this might sound stupid but..) it executes Class2 static procedure even though the variable is declared as type class1 (parent) variable, should that not be possible if Virtual Method Table isn't created?

Thaddy

  • Hero Member
  • *****
  • Posts: 14201
  • Probably until I exterminate Putin.
Re: Static Class Procedure cannot be virtual..
« Reply #3 on: July 19, 2018, 01:27:26 pm »
I was mistaken. class methods have their own VMT as per the above link.
In trunk I get the expected error:
Code: Bash  [Select][+][-]
  1. fpc -Fl/usr/lib/gcc/arm-linux-gnueabihf/6.3.0 -Fu/usr/local/lib/fpc/3.1.1/units/arm-linux/* -Fu/home/pi/synapse/  "vmtstatic.pas" (in directory: /home/pi)
  2. Compiling vmtstatic.pas
  3. vmtstatic.pas(6,47) Error: Procedure directive "STATIC" cannot be used with "VIRTUAL"
  4. vmtstatic.pas(9,49) Error: Procedure directive "STATIC" cannot be used with "VIRTUAL"
  5. vmtstatic.pas(29) Fatal: There were 2 errors compiling module, stopping
  6. Fatal: Compilation aborted
  7. Error: /usr/local/bin/ppcarm returned an error exitcode
  8. Compilation failed.
  9.  
Maybe 3.0.4. simply ignores and drops static.

Tested with:
Code: Pascal  [Select][+][-]
  1. {$ifdef fpc}{$mode delphi}{$H+}{$endif}
  2. {$macro on}{$define showmessage:=writeln}
  3. type
  4.  TClass1 = class
  5.     class procedure classProcStatic; virtual; static;
  6.   end;
  7.   TClass2 = class (TClass1)
  8.     class procedure classProcStatic;  override; static;
  9.   end;
  10.  
  11. class procedure TClass2.classProcStatic;
  12. begin
  13.   ShowMessage('Class 2 Static Procedure');
  14. end;
  15.  
  16. class procedure TClass1.classProcStatic;
  17. begin
  18.   ShowMessage('Class 1 Static Procedure');
  19. end;
  20.  
  21. var
  22.   c1 : TClass1;
  23. begin
  24.   c1 := TClass2.Create;
  25.   c1.classProcStatic;
  26.   c1.Free;
  27. end.

« Last Edit: July 19, 2018, 01:38:20 pm by Thaddy »
Specialize a type, not a var.

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Re: Static Class Procedure cannot be virtual..
« Reply #4 on: July 19, 2018, 01:38:51 pm »
Thanks Thaddy, but .. (now this might sound stupid but..) it executes Class2 static procedure even though the variable is declared as type class1 (parent) variable, should that not be possible if Virtual Method Table isn't created?
VMT of class doesn't have to be "somehow" created. You declared two classes and each class has its own VMT, so they exist.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

darksky666

  • New Member
  • *
  • Posts: 38
Re: Static Class Procedure cannot be virtual..
« Reply #5 on: July 19, 2018, 01:53:30 pm »
I was mistaken. class methods have their own VMT as per the above link.
In trunk I get the expected error:
Code: Bash  [Select][+][-]
  1. fpc -Fl/usr/lib/gcc/arm-linux-gnueabihf/6.3.0 -Fu/usr/local/lib/fpc/3.1.1/units/arm-linux/* -Fu/home/pi/synapse/  "vmtstatic.pas" (in directory: /home/pi)
  2. Compiling vmtstatic.pas
  3. vmtstatic.pas(6,47) Error: Procedure directive "STATIC" cannot be used with "VIRTUAL"
  4. vmtstatic.pas(9,49) Error: Procedure directive "STATIC" cannot be used with "VIRTUAL"
  5. vmtstatic.pas(29) Fatal: There were 2 errors compiling module, stopping
  6. Fatal: Compilation aborted
  7. Error: /usr/local/bin/ppcarm returned an error exitcode
  8. Compilation failed.
  9.  
Maybe 3.0.4. simply ignores and drops static.

Thanks for checking it with the trunk, so happy to read that the next release will not allow it.. But it seems FPC doesn't drop the keyword "static" -- because in a class method you can use something like "Self.ClassName" but "virtual static class procedure" doesn't allow it (I am assuming it's because static isn't dropped?)..

 

TinyPortal © 2005-2018