Delphi 中的无类型/无类型参数

发布于 2024-08-16 05:01:13 字数 169 浏览 5 评论 0原文

像 TStringStream 类中没有类型的参数是什么类型:

function Read(var Buffer; Count: Longint): Longint; override;

Buffer 参数的类型是什么(它是 Pointer 类型吗?)。

What type are parameters without type like in the class TStringStream:

function Read(var Buffer; Count: Longint): Longint; override;

What is the type of Buffer parameter (is it a type of Pointer ?).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

眼趣 2024-08-23 05:01:13

几年前我写了一篇关于这个主题的文章:

什么是非类型化参数?

非类型化参数在几种情况下使用;您询问的 TStream.Read 方法与我写的 Move 过程最匹配。这是摘录:

procedure Move(const Source; var Dest; Count: Integer);

Move 过程从任意变量复制数据
到任何其他变量中。它需要接受来源和目的地
所有类型,这意味着它不能需要任何单一类型。程序
不会修改为 Source 传递的变量值,因此
参数的声明使用 const 而不是 var,这是
对于无类型参数更常见的修饰符。

对于 TStream.Read,源是流的内容,因此您不会将其作为参数传递,但目标是中所示的 Buffer 参数的问题。您可以为该参数传递您想要的任何变量类型,但这意味着您需要小心。确保流的内容确实是您提供的参数类型的有效值是您的工作,而不是编译器的工作。

请阅读我的文章的其余部分,了解 Delphi 使用非类型化参数的更多情况。

I wrote an article about this very topic a few years ago:

What is an untyped parameter?

Untyped parameters are used in a few situations; the TStream.Read method you ask about most closely matches with the Move procedure I wrote about. Here's an excerpt:

procedure Move(const Source; var Dest; Count: Integer);

The Move procedure copies data from an arbitrary variable
into any other variable. It needs to accept sources and destinations of
all types, which means it cannot require any single type. The procedure
does not modify the value of the variable passed for Source, so that
parameter’s declaration uses const instead of var, which is the
more common modifier for untyped parameters.

In the case of TStream.Read, the source is the stream's contents, so you don't pass that in as a parameter, but the destination is the Buffer parameter shown in the question. You can pass any variable type you want for that parameter, but that means you need to be careful. It's your job, not the compiler's, to ensure that the contents of the stream really are a valid value for the type of parameter you provide.

Read the rest of my article for more situations where Delphi uses untyped parameters.

杀手六號 2024-08-23 05:01:13

查看 Delphi 帮助中的“无类型参数”。

您可以传入任何类型,但必须在实现中对其进行强制转换。帮助表明您不能向其传递数字或无类型数字常量。所以基本上你必须知道需要什么类型,而编译器无法帮助你,所以你需要一个充分的理由来这样做。我想如果您需要该方法来处理不兼容的类型,那么它可能很有用,但是您可以为每个预期类型编写几个重载版本,我建议将其作为更好的解决方案。

Check out the Delphi help for "Untyped parameters"

You can pass in any type, but you have to cast it in the implementation. Help says that you cannot pass it a numeral or untyped numeric constant. So basically you have to be know what type to expect, and compiler can not help you, so you need a good reason to do it this way. I suppose it could be of use if you need the method to handle incompatible types, but then again you could write several overloaded versions for each expected type, I would suggest that as a better solution.

一影成城 2024-08-23 05:01:13

也许令人惊讶的是,将取消引用的指针作为无类型参数传递是合法的。而且指针本身甚至不必有类型。

procedure SomeMethod(var aParameter);
  ∶

procedure CallSomeMethod(aIsInteger : Boolean);
type
  buffer    : Pointer;
  intValue  : Integer;
  realValue : Single;
begin
  if aIsInteger then
  begin
    buffer := @intValue;
  end
  else
  begin
    buffer := @realValue;
  end;
  SomeMethod(buffer^);

当然,如果 SomeMethod() 的参数是一个指针,那么可能会更容易,但这可能不受您的控制。

Perhaps surprisingly, it is legal to pass a dereferenced pointer as an untyped parameter. And the pointer itself doesn't even have to have a type.

procedure SomeMethod(var aParameter);
  ∶

procedure CallSomeMethod(aIsInteger : Boolean);
type
  buffer    : Pointer;
  intValue  : Integer;
  realValue : Single;
begin
  if aIsInteger then
  begin
    buffer := @intValue;
  end
  else
  begin
    buffer := @realValue;
  end;
  SomeMethod(buffer^);

Of course it would probably have been easier if the parameter to SomeMethod() had been a pointer, but this might not be under your control.

一个人的夜不怕黑 2024-08-23 05:01:13

参数列表中的 var通过引用调用。它可以键入,例如Listview 的OnChanging 处理程序中的AllowChange 参数:

procedure TSomeForm.LVOnChanging(Sender: TObject; ...; var AllowChange: Boolean);
begin
  if SomeProblemOccurred then
    AllowChange := False;
end;

非键入,如您的示例中所示。

var in a parameter list is the Delphi syntax for call by reference. It can be typed as e.g. the AllowChange parameter in the OnChanging handler of an Listview:

procedure TSomeForm.LVOnChanging(Sender: TObject; ...; var AllowChange: Boolean);
begin
  if SomeProblemOccurred then
    AllowChange := False;
end;

or untyped as in your example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文