Delphi 中 TSomething 的默认参数值

发布于 2024-09-24 07:08:14 字数 377 浏览 10 评论 0原文

我想知道这在 Delphi 中是否可行(或者是否有一种干净的方法):

type
 TSomething = record
  X, Y : Integer;
 end;

GetSomething( x, y ) ->返回包含这些值的记录。

...然后你有这个函数以 TSomething 作为参数,并且你想将其默认为

function Foo( Something : TSomething = GetSomething( 1, 3 );

编译器在这里吐出一个错误,但是我不确定是否有解决方法!

这可以做到吗?

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 ) -> Returns record with those values.

... 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!

Can this be done?

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

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

发布评论

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

评论(4

神妖 2024-10-01 07:08:14

使用重载:

procedure Foo(const ASomething: TSomething); overload;
begin
  // do something with ASomething
end;

procedure Foo; overload;
begin
  Foo(GetSomething(1, 3));
end;

Use overloading:

procedure Foo(const ASomething: TSomething); overload;
begin
  // do something with ASomething
end;

procedure Foo; overload;
begin
  Foo(GetSomething(1, 3));
end;
耳根太软 2024-10-01 07:08:14

最简单的方法是使用重载过程:

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.

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.
十六岁半 2024-10-01 07:08:14

使用类而不是记录,像这样的东西就可以做到:

TSomething = class
public 
  X: integer;
  Y: integer
end;

procedure Foo(Something: TSomething = nil);
begin
  if (Something = nil) then
    Something := GetSomething(1, 3);
  ...
end;

Use a class instead of a record and something like this would do it:

TSomething = class
public 
  X: integer;
  Y: integer
end;

procedure Foo(Something: TSomething = nil);
begin
  if (Something = nil) then
    Something := GetSomething(1, 3);
  ...
end;
十秒萌定你 2024-10-01 07:08:14

如果您使用指针而不是记录类型,则可以使用 nil 作为默认值:

type
  TSomething = record
    X, Y : Integer;
  end;

  PSomething = ^TSomething;

function Foo(Something: PSomething = nil);

实际上,将指针作为参数传递通常更快,因为它避免了复制内存块。

If you use a pointer instead of the record type you can use nil as default value:

type
  TSomething = record
    X, Y : Integer;
  end;

  PSomething = ^TSomething;

function Foo(Something: PSomething = nil);

Actually, passing pointers as parameters is usually faster because it avoids copying blocks of memory.

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