delphi - [ref] 在 VCL 应用程序中做什么?

标签 delphi parameter-passing pass-by-reference custom-attributes

我正在使用 Delphi 10 Seattle 开发一个 VCL 应用程序,并通过 IDE 创建了一个 TDBGrid 事件处理程序,当时我注意到 Delphi 为 Rect 添加了一个 Ref 自定义属性。参数:

procedure TfrmXxx.yyyDrawColumnCell(Sender: TObject;
  const [Ref] Rect: TRect; DataCol: Integer; Column: TColumn;
  State: TGridDrawState);
begin
  //
end;
  • IDE 何时或为何决定插入此内容?
  • 它对 VCL 应用程序有影响吗?
<小时/>

更新

对于那些无法重现该行为的人来说,这是一个视频: enter image description here

最佳答案

文档中提到:

Constant parameters may be passed to the function by value or by reference, depending on the specific compiler used. To force the compiler to pass a constant parameter by reference, you can use the [Ref] decorator with the const keyword.

参见Constant Parameters

When or why does the IDE decide to insert this?

IDE 永远不会插入此内容。它只是复制事件处理程序的声明。编写事件处理程序的人将 pass by[ref]erence 标记放在那里。

Does it have any effect in a VCL app?

是的。
如果将 8 字节参数标记为 const,它通常会在 x64 中按值传递,在 x86 中按引用传递。
将其声明为 const [ref] 将强制在这两种情况下通过引用传递它。
在进行内联汇编和多线程代码时非常有用。
在引入 const [ref] 之前,我们被迫使用 var 而不是 const 来达到相同的效果。

关于delphi - [ref] 在 VCL 应用程序中做什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36940456/

相关文章:

delphi - 如何保护delphi应用程序的源代码?

iphone - 火猴到 iPhone

java - 如何将 JPanel 作为参数传递给方法?

javascript - 如何将变量按值传递给匿名 JavaScript 函数?

c++ - 为什么Clang会为引用和非空指针参数生成不同的代码?

java - 如何应对无法通过引用传递?

delphi - 如何复制位图图像上圆圈中的所有像素并将其粘贴到图像上的另一个位置?

excel - 可在 VBA 外部使用的 Excel RefEdit 控件的替代品

java - 在组件之间传递值

c# - 为什么要使用 params 关键字?