德尔福最大的错误或限制.常数整数值?

前端之家收集整理的这篇文章主要介绍了德尔福最大的错误或限制.常数整数值?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
参见英文答案 > E2099 Overflow in conversion or arithmetic operation1个
const
  minDriveFreeSpace: Int64 = 1024*1024*1024*99;

var
  minDriveFreeSpace: Int64;
begin
  minDriveFreeSpace := 1024*1024*1024*99;

会发出:

[dcc32 Error] DataStoreLocator.pas(92): E2099 Overflow in conversion or arithmetic operation

这是Delphi最大的错误还是限制.常数整数值?

解决方法

您需要在右侧将至少一个值强制转换为Int64.例如,这两个在XE6上编译得非常好:
const
  minDriveFreeSpace = Int64(1024) * 1024 * 1024 * 99;

var
  minDriveFreeSpace2: Int64;
begin
  minDriveFreeSpace2 := Int64(1024)*1024*1024*99;

请注意,它可以是任何一个施放的右值.例如,这也同样有效:

const
  minDriveFreeSpace = 1024 * 1024 * 1024 * Int64(99);

这在Delphi language guide中有记录(虽然相当差) – 强调我的:

In general,arithmetic operations on integers return a value of type Integer,which is equivalent to the 32-bit LongInt. Operations return a value of type Int64 only when performed on one or more Int64 operands. Therefore,the following code produces incorrect results:

var
I: Integer;
J: Int64;
... 
I := High(Integer);
J := I + 1;

To get an Int64 return value in this situation,cast I as Int64:

...
J := Int64(I) + 1;

猜你在找的Delphi相关文章