Delphi中TSomething的默认参数值 [英] Default parameter value for a TSomething in Delphi
本文介绍了Delphi中TSomething的默认参数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道在Delphi中是否有可能(或者周围是否有干净的方法):
I'd like to know if this is possible in Delphi (or if there's a clean way around it):
type
TSomething = record
X, Y : Integer;
end;
GetSomething( x, y )
->返回带有这些值的记录.
GetSomething( x, y )
-> Returns record with those values.
...,然后您具有以TSomething
作为参数的此功能,并且您希望将其默认设置为
... and then you have this function with TSomething
as parameter, and you want to default it as
function Foo( Something : TSomething = GetSomething( 1, 3 );
编译器在这里吐出一个错误,但是我不确定是否有解决方法!
The compiler spits an error here, however I'm not sure if there's a way around it!
可以做到吗?
推荐答案
最简单的方法是使用重载过程:
The easiest way is to use overloaded procedures:
program TestOverloading;
{$APPTYPE CONSOLE}
uses
SysUtils;
type
TSomething = record
X,Y : integer;
end;
const
cDefaultSomething : TSomething = (X:100; Y:200);
procedure Foo(aSomething : TSomething); overload;
begin
writeln('X:',aSomething.X);
writeln('Y:',aSomething.Y);
end;
procedure Foo; overload;
begin
Foo(cDefaultSomething);
end;
begin
Foo;
readln;
end.
这篇关于Delphi中TSomething的默认参数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文