gpt4 book ai didi

delphi - Delphi中如何定义一个包含其他常量记录的常量记录?具体案例: matrix using vectors

转载 作者:行者123 更新时间:2023-12-03 14:57:30 24 4
gpt4 key购买 nike

假设我在一个单元中有一个简单的记录,例如:

TVector2D = record
public
class function New(const x, y: Accuracy): TVector2D; static;
public
x, y: Accuracy;
end;

然后,我在同一单元中拥有使用一组上述记录构建的第二条记录,例如:

TMatrix3D = record
public
class function New(const row1, row2, row3: TVector3D): TMatrix3D; static;
public
Index : array [0..2] of TVector3D;
end;

然后我定义轴方向常数如下:

//Unit vector constants
const
iHat : TVector3D = (x: 1; y: 0; z: 0);
jHat : TVector3D = (x: 0; y: 1; z: 0);
kHat : TVector3D = (x: 0; y: 0; z: 1);

我现在想使用上述常量定义另一个常量,例如:

  identity : TMatrix3D = (row1: iHat; row2: jHat; row3: kHat);

然而上述尝试并没有奏效。我该如何在 Delphi XE2 中执行此操作?

非常感谢您的努力。 :-)

最佳答案

这是不可能的。在常量记录声明中,成员值必须为 constant expressions 。也就是说,您不能像您尝试的那样使用类型化常量。

documentation是这样说的,我强调的是:

Record Constants

To declare a record constant, specify the value of each field - as fieldName: value, with the field assignments separated by semicolons - in parentheses at the end of the declaration. The values must be represented by constant expressions.

所以你需要这样声明:

const
identity: TMatrix3D = (Index:
((x: 1; y: 0; z: 0),
(x: 0; y: 1; z: 0),
(x: 0; y: 0; z: 1))
);

不得不重复自己的话令人沮丧,但我怀疑这是你能做的最好的事情。

关于delphi - Delphi中如何定义一个包含其他常量记录的常量记录?具体案例: matrix using vectors,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28699518/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com