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.

简而言之 - 我没有理由。

智能推荐

注意!

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



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

赞助商广告