Delphi的靜態關鍵字是否在本機代碼中有任何意義?

[英]Does Delphi's static keyword have any point in native-only code?


My understanding is that the static keyword was introduced for compatibility with .NET (along with strict)

我的理解是引入了static關鍵字以與.NET兼容(以及嚴格)

class TExample
  class procedure First;
  class procedure Second; static;

The differences between procedures First and Second are :-

第一和第二程序之間的區別是: -

  1. First can be overridden in a descendant class
  2. 首先可以在后代類中重寫

  3. First passes an implicit self parameter referencing the TExample class.
  4. 首先傳遞一個引用TExample類的隱式self參數。

Class procedure Second cannot be overridden and passes no parameters and is thus .NET compatible. So is there any point in using the static keyword in native-only code now that there is a divergence between Delphi & Prism syntax?

類過程第二個不能被覆蓋並且不傳遞參數,因此是.NET兼容的。那么在現有代碼中使用static關鍵字是否有任何意義,因為Delphi和Prism語法之間存在分歧?

2 个解决方案

#1


Static class methods have no hidden class reference argument. Because of this, they are assignment compatible with plain old function pointers, and can therefore be used for interaction with the Windows API and other C APIs. Example:

靜態類方法沒有隱藏的類引用參數。因此,它們與普通的舊函數指針兼容,因此可用於與Windows API和其他C API交互。例:

type
  TForm = class
  private
    class function NonStaticWndProc (wnd: HWND; Message: Cardinal;
      wParam: WPARAM; lParam: LPARAM): LRESULT;
    class function StaticWndProc (wnd: HWND; Message: Cardinal;
      wParam: WPARAM; lParam: LPARAM): LRESULT; static;
    procedure RegisterClass;
  end;

procedure TForm.RegisterClass;
type
  TWndProc = function (wnd: HWND; Message: Cardinal;
    wParam: WPARAM; lParam: LPARAM): LRESULT;
var
  WP: TWndProc;
  WindowClass: WNDCLASS;
begin
  //WP := NonStaticWndProc; // doesn't work
  WP := StaticWndProc; // works
  // ...
  TWndProc (WindowClass.lpfnWndProc) := WP;
  Windows.RegisterClass (WindowClass);
end;

(Of course, you could have used a global function instead, but other than global functions, static class functions have a clear association with a class.)

(當然,您可以使用全局函數,但除了全局函數之外,靜態類函數與類有明確的關聯。)

#2


with static, it is a little bit faster. There's one add esp, -8 in method First which is not there in Second.

靜態,它有點快。在方法First中有一個添加esp,-8,在Second中沒有。

program staticTest;

{$APPTYPE CONSOLE}

uses
  SysUtils;

type
  TExample=class
    class procedure First;
    class procedure Second; static;
  end;


{ TExample }

class procedure TExample.First;
var
  i : Integer;
begin
  i:=61374;
end;

class procedure TExample.Second;
var
  I : Integer;
begin
  i:=44510;
end;

begin
  { TODO -oUser -cConsole Main : Hier Code einfügen }
  TExample.First;
  TExample.Second;
end.

First:

staticTest.dpr.20: begin
00408474 55               push ebp
00408475 8BEC             mov ebp,esp
00408477 83C4F8           add esp,-$08  ;This is the line I mentioned
0040847A 8945FC           mov [ebp-$04],eax
staticTest.dpr.21: i:=61374;
0040847D C745F8BEEF0000   mov [ebp-$08],$0000efbe
staticTest.dpr.22: end;
00408484 59               pop ecx
00408485 59               pop ecx
00408486 5D               pop ebp
00408487 C3               ret 

Second:

staticTest.dpr.27: begin
00408488 55               push ebp
00408489 8BEC             mov ebp,esp
0040848B 51               push ecx
staticTest.dpr.28: i:=44510;
0040848C C745FCDEAD0000   mov [ebp-$04],$0000adde
staticTest.dpr.29: end;
00408493 59               pop ecx
00408494 5D               pop ebp
00408495 C3               ret 
00408496 8BC0             mov eax,eax

In short - I see no reason.

簡而言之 - 我沒有理由。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2009/07/17/7202e3f1f3f8a566c4a908fefeaf3a1b.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com