Delphi中的非typed / typeless参数 [英] Untyped / typeless parameters in Delphi

查看:146
本文介绍了Delphi中的非typed / typeless参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么类型的参数没有类型,如类TStringStream:

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

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

Buffer参数的类型是什么(指针类型是什么?)

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

推荐答案

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

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


什么是无类型参数?

在几种情况下使用非标准参数;您所要求的 TStream.Read 方法与我写的 Move 程序最相符。这是一个摘录:

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);

Move 变量
转换成任何其他变量。它需要接受
所有类型的源和目的地,这意味着它不需要任何单一类型。程序
不修改为 Source 传递的变量的值,因此
参数的声明使用 const 而不是 var ,这是非类型参数的
更常见的修饰符。

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.

TStream.Read 的情况下,源是流的内容,因此您不会将其作为参数传递,但是目的地是问题中显示的缓冲区参数。您可以传递所需的该参数的变量类型,但这意味着您需要小心。这是您的作业,而不是编译器,以确保流的内容真正是您提供的参数类型的有效值。

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.

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

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

这篇关于Delphi中的非typed / typeless参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆