在 Delphi 7 中,为什么我可以为 const 赋值? [英] In Delphi 7, why can I assign a value to a const?

查看:26
本文介绍了在 Delphi 7 中,为什么我可以为 const 赋值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将一些 Delphi 代码从一个项目复制到另一个项目,发现它在新项目中无法编译,但在旧项目中可以.代码如下所示:

I copied some Delphi code from one project to another, and found that it doesn't compile in the new project, though it did in the old one. The code looks something like this:

procedure TForm1.CalculateGP(..)
const
   Price : money = 0;
begin
   ...
   Price := 1.0;
   ...
end;

所以在新项目中,Delphi 抱怨无法分配左侧"——可以理解!但是这段代码在旧项目中编译.所以我的问题是,为什么?是否有允许重新分配 const 的编译器开关?这甚至是如何工作的?我认为 const 在编译时被它们的值替换了吗?

So in the new project, Delphi complains that "left side cannot be assigned to" - understandable! But this code compiles in the old project. So my question is, why? Is there a compiler switch to allow consts to be reassigned? How does that even work? I thought consts were replaced by their values at compile time?

推荐答案

你需要打开可赋值的类型常量.项目 -> 选项 -> 编译器 -> 可赋值的类型常量

You need to turn assignable typed constants on. Project -> Options -> Compiler -> Assignable typed Constants

您还可以将 {$J+}{$WRITEABLECONST ON} 添加到 pas 文件中,这可能会更好,因为即使您移动它也可以工作文件到另一个项目.

Also you can add {$J+} or {$WRITEABLECONST ON} to the pas file, which is probably better, since it'll work even if you move the file to another project.

这篇关于在 Delphi 7 中,为什么我可以为 const 赋值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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