相关疑难解决方法(0)

真的C静态局部变量替换?

只是试图在ObjectPascal/Delphi中实现C/C++静态局部变量的类似功能.我们在C中有以下功能:

bool update_position(int x, int y)
{
    static int dx = pow(-1.0, rand() % 2);
    static int dy = pow(-1.0, rand() % 2);

    if (x+dx < 0 || x+dx > 640)
        dx = -dx;
    ...
    move_object(x+dx, y+dy);
    ...
}
Run Code Online (Sandbox Code Playgroud)

使用类型常量作为静态变量替换的等效ObjectPascal代码无法编译:

function UpdatePosition(x,y: Integer): Boolean;
const
  dx: Integer = Trunc( Power(-1, Random(2)) );  // error E2026
  dy: Integer = Trunc( Power(-1, Random(2)) );
begin
  if (x+dx < 0) or (x+dx > 640) then
    dx := -dx;
  ...
  MoveObject(x+dx, y+dy);
  ...
end; …
Run Code Online (Sandbox Code Playgroud)

delphi pascal

6
推荐指数
1
解决办法
4363
查看次数

标签 统计

delphi ×1

pascal ×1