对于最新的 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/