delphi - const 记录参数的 [Ref] 属性有用吗?

标签 delphi delphi-10.1-berlin

对于最新的 Delphi 版本 (Berlin/10.1/24),[Ref] 属性真的有必要吗?

我问这个是因为 online doc说:

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.

最佳答案

这与文档中描述的非常相似。如果您有理由强制通过引用传递参数,则可以使用 [ref]。我能想到的一个例子是互操作。假设您正在调用一个定义如下的 API 函数:

typedef struct {
    int foo;
} INFO;

int DoStuff(const INFO *lpInfo);

在 Pascal 中,您可能希望像这样导入它:

type
  TInfo = record
    foo: Integer;
  end;

function DoStuff(const Info: TInfo): Integer; cdecl; external libname;

但是由于 TInfo 很小,编译器可能会选择按值传递结构。所以你可以用[ref]注释来强制编译器将参数作为引用传递。

function DoStuff(const [ref] Info: TInfo): Integer; cdecl; external libname;

关于delphi - const 记录参数的 [Ref] 属性有用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40929338/

相关文章:

delphi - FireMonkey Android加速传感器CPU使用率高

delphi - Delphi中如何处理带空格的参数?

android - 如何使用Delphi实现android和ios的共享文本选项

forms - 如何创建一个包含多个 'child'表单的delphi表单,这些表单可以移动/调整大小并显示激活

c++ - GetTokenInformation() 第一次调用。做什么的?

multithreading - 如何从主线程完全终止并行任务?

delphi - 如何可靠地检测剪贴板上的 RITCHTEXT 格式?

listview - 如何通过代码使ListView VCL折叠/展开?

delphi - 在 Delphi 中的窗体上切换大量控件的控件类型(但不是名称)

delphi - 来自 Delphi 2006 的 EXE : Buttons/Checkbox/Radios not visible until mouse is hovered (on Vista/Win7)