Delphi:声明包含常量数组的常量记录类型
我有许多常量数组并不都具有相同数量的元素。
为了存储这些数组,我声明了一个足够大的数组类型来存储(或引用?)这些数组中最大的每个元素:
type
TElements = array [1 .. 1024] of Single;
这些 TElements 数组中的每一个都在逻辑上与另一个 TElements 数组相关联,确实 具有相同数量的元素。
因此,为了将这些大小相等的数组配对,我将记录类型声明为:
type
TPair = record
n : Integer; // number of elements in both TElements arrays
x : ^TElements;
y : ^TElements;
end;
然后,我定义包含常量 TElements 数组对的常量 TPair 记录:
const
ElementsA1 : array [1 .. 3] of Single = (0.0, 1.0, 2.0);
ElementsA2 : array [1 .. 3] of Single = (0.0, 10.0, 100.0);
ElementsA : TPair =
(
n : 3;
x : @ElementsA1;
y : @ElementsA2;
);
ElementsB1 : array [1 .. 4] of Single = (0.0, 1.0, 2.0, 3.0);
ElementsB2 : array [1 .. 4] of Single = (0.0, 10.0, 100.0, 1000.0);
ElementsB : TPair =
(
n : 4;
x : @ElementsB1;
y : @ElementsB2;
);
这似乎是引用数组数据的低效方法(也许不是,我不知道) 。
我想维护一个包含两个常量数组的常量数据类型(“对”数据类型)。
在每个“对”中,两个数组都保证具有相同数量的元素。
但是,不能保证一个“对”中的数组元素数量等于任何其他“对”中的数组元素数量。
有没有办法声明常量“对”数据类型,以便包含的数组大小由常量数组定义确定?
理想情况下,我想摆脱 TElements 类型和尴尬的指针。如果它能编译的话,这样的东西会很酷:
type
TPair = record
x : array of Single;
y : array of Single;
end;
const
ElementsA : TPair =
(
x : (0.0, 1.0, 2.0);
y : (0.0, 10.0, 100.0);
);
ElementsB : TPair =
(
x : (0.0, 1.0, 2.0, 3.0);
y : (0.0, 10.0, 100.0, 1000.0);
);
但我想由于数组被声明为动态数组,它不想在运行时之前为它们分配内存?
I have many constant arrays that do not all have the same number of elements.
To store these arrays, I have declared an array type large enough to store (or reference?) every element of the largest of these arrays:
type
TElements = array [1 .. 1024] of Single;
Each of these TElements arrays are logically associated with one other TElements array that does have the same number of elements.
So to pair up these equally-sized arrays, I have declared a record type as:
type
TPair = record
n : Integer; // number of elements in both TElements arrays
x : ^TElements;
y : ^TElements;
end;
I am then defining constant TPair records containing the constant TElements array pairs:
const
ElementsA1 : array [1 .. 3] of Single = (0.0, 1.0, 2.0);
ElementsA2 : array [1 .. 3] of Single = (0.0, 10.0, 100.0);
ElementsA : TPair =
(
n : 3;
x : @ElementsA1;
y : @ElementsA2;
);
ElementsB1 : array [1 .. 4] of Single = (0.0, 1.0, 2.0, 3.0);
ElementsB2 : array [1 .. 4] of Single = (0.0, 10.0, 100.0, 1000.0);
ElementsB : TPair =
(
n : 4;
x : @ElementsB1;
y : @ElementsB2;
);
This seems like an inefficient way to reference the array data (maybe not, I dunno).
I would like to maintain a single constant data type (a "pair" data type) that contains two constant arrays.
Within each "pair", both arrays are guaranteed to have the same number of elements.
However, it can not be guaranteed that the number of array elements in one "pair" will equal the number of array elements in any other "pair".
Is there a way to declare a constant "pair" data type so that the contained array sizes are determined by the constant array definition?
Ideally, I would like to get rid of the TElements type and the awkward pointers. Something like this would be cool if it would compile:
type
TPair = record
x : array of Single;
y : array of Single;
end;
const
ElementsA : TPair =
(
x : (0.0, 1.0, 2.0);
y : (0.0, 10.0, 100.0);
);
ElementsB : TPair =
(
x : (0.0, 1.0, 2.0, 3.0);
y : (0.0, 10.0, 100.0, 1000.0);
);
But I guess since the arrays are declared as dynamic arrays, it doesn't want to allocate memory for them before runtime?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,遗憾的是这是不可能的。您必须在方括号内声明数组的大小。
No, sadly this is not possible. You have to declare the size of your array inside the square brackets.