德尔福数组初始化 [英] Delphi array initialization

查看:216
本文介绍了德尔福数组初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我都这样了,它吮吸:

I currently have this, and it sucks:

type TpointArray = array [0..3] of Tpoint;

class function rotationTable.offsets(pType, rotState, dir: integer): TpointArray;
begin

  Result[0] := point(1, 1);
  Result[1] := point(1, 2);
  Result[2] := point(1, 1);
  Result[3] := point(1, 1);
end;

但相反,我想要做的是这样的:

class function rotationTable.offsets(pType, rotState, dir: integer): TpointArray;
begin
   Result := [Point(1,1), Point(1,2), Point(1,1), Point(1,1)];
end;

不过,汇编,它抱怨说,[1,2,3,4]语法只能为​​整数的工作。

However, on compilation, it complains that the [1, 2, 3, 4] syntax can only work for Integers.

有没有一种方法来实例化/初始化类似于我想要的方式?

Is there a way to instantiate/initialize an array of Tpoint similar to the way that i want?

推荐答案

记录阵列可以在常量前pressions被intialised:

Arrays of records can be intialised in const expressions:

const
  Points : TPointArray = ((X: 1; Y: 1), (X:1; Y:2), (X:1; Y:1), (X:1; Y:1));

class function rotationTable.offsets(pType, rotState, dir: integer): TpointArray;
begin
   Result := Points;
end;

在XE7能够填补记录动态数组是这样的:

In XE7 it is possible to fill a dynamic array of records like this:

function GetPointArray: TArray<TPoint>;
begin
  Result := [Point(1,1),Point(1,2),Point(1,1),Point(1,1)];
end;

这篇关于德尔福数组初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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