为什么包含程序时德尔福记录的大小没有增加?(Why the size of a Delphi record is not increased when a procedure is included?

系统教程 行业动态 更新时间:2024-06-14 16:57:17
为什么包含程序时德尔福记录的大小没有增加?(Why the size of a Delphi record is not increased when a procedure is included?)

我有两个相同字段的记录,其中一个记录有一组过程。 为什么两个记录的大小相同?

{$APPTYPE CONSOLE} {$R *.res} uses System.SysUtils; type TData = record Age : Byte; Id : Integer; end; TData2 = record Age : Byte; Id : Integer; procedure foo1; procedure foo2; procedure foo3; end; procedure TData2.foo1; begin end; procedure TData2.foo2; begin end; procedure TData2.foo3; begin end; begin try Writeln('SizeOf(TData) = '+ IntToStr(SizeOf(TData))); Writeln('SizeOf(TData2) = '+ IntToStr(SizeOf(TData2))); Readln; except on E: Exception do Writeln(E.ClassName, ': ', E.Message); end; end.

I have two records with the same fields and one of them has a set of procedures. Why the Size of both records is the same?

{$APPTYPE CONSOLE} {$R *.res} uses System.SysUtils; type TData = record Age : Byte; Id : Integer; end; TData2 = record Age : Byte; Id : Integer; procedure foo1; procedure foo2; procedure foo3; end; procedure TData2.foo1; begin end; procedure TData2.foo2; begin end; procedure TData2.foo3; begin end; begin try Writeln('SizeOf(TData) = '+ IntToStr(SizeOf(TData))); Writeln('SizeOf(TData2) = '+ IntToStr(SizeOf(TData2))); Readln; except on E: Exception do Writeln(E.ClassName, ': ', E.Message); end; end.

最满意答案

这是因为记录本身只携带构成记录的数据,没有程序或功能。 程序和函数是一种语法糖,以避免将记录本身作为参数传递:编译器为您自动添加的变量。

您在记录中声明的每个方法都有记录本身的另一个参数,例如:

TData2 = record Age : Byte; Id : Integer; procedure Foo1; procedure Foo2(SomeParam: Integer); end;

被改变为相当于:

PData2 = ^TData2; TData2 = record Age : Byte; Id : Integer; end; procedure TData2_Foo1(Self: PData2); procedure TData2_Foo2(Self: PData2; SomeParam: Integer);

结束您所做的每个呼叫也会发生变化,例如:

var Data: TData2; begin Data.Foo1; Data.Foo2(1); end;

被改变为相当于:

var Data: TData2; begin TData2_Foo1(@Data); TData2_Foo1(@Data, 1); end;

我手头没有Delphi来检查参数是添加在参数列表的开头还是结尾,但我希望你明白。

当然,这没有真正的语法,因为它是由编译器即时完成的,因此,例如,过程名称不会改变。 我试图让我的回答容易理解。

That's because the record itself only carries with the data that composes the record and no procedures or functions. The procedures and functions are a kind of syntactic sugar to avoid passing the record itself as a parameter: the self variable that is automagically added by the compiler for you.

Each method you declare in a record have another parameter for the record itself, for example:

TData2 = record Age : Byte; Id : Integer; procedure Foo1; procedure Foo2(SomeParam: Integer); end;

is changed to something equivalent to:

PData2 = ^TData2; TData2 = record Age : Byte; Id : Integer; end; procedure TData2_Foo1(Self: PData2); procedure TData2_Foo2(Self: PData2; SomeParam: Integer);

end each call you make is also changed, for example:

var Data: TData2; begin Data.Foo1; Data.Foo2(1); end;

is changed for something equivalent to:

var Data: TData2; begin TData2_Foo1(@Data); TData2_Foo1(@Data, 1); end;

I have no Delphi at hand to check if the parameter is added at the beginning or at the end of your parameter list, but I hope you get the idea.

Of course there's no real syntax for this, since it is done on the fly by the compiler and thus, for example, the procedure names are not changed. I did that in a try to make my answer easy to understand.

更多推荐

本文发布于:2023-04-12 19:56:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/a244af8e7f49dcd4bcbb777476c80ea9.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:大小   德尔福   程序   size   Delphi

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!