gpt4 book ai didi

delphi - 为什么 Delphi 编译器不针对重新定义的常量发出警告?

转载 作者:行者123 更新时间:2023-12-03 14:44:04 26 4
gpt4 key购买 nike

我的一位同事遇到了一个突然“改变值”的常量;
结果,它被重新声明了:

unit Unit1;

interface

const
MyConstant = 1;

implementation

end.

--

unit Unit2;

interface

const
MyConstant = 2;

implementation

end.

--

Uses Unit1, Unit2;
// Uses Unit2, Unit1;

procedure TFrmRedefineConstant.FormShow(Sender: TObject);
begin
ShowMessage('MyConstant: ' + IntToStr(MyConstant));
end;

这显示2。如果交换 Uses 语句中的单位顺序,则会显示 1

很好,但是为什么 Delphi 编译器不警告重复的常量名称(这会很有帮助)?
我可以做些什么来启用警告(看起来不是那样)。

最佳答案

因为 Delphi 记录了范围规则。来自语言指南:

The order in which units appear in the uses clause determines the order of their initialization and affects the way identifiers are located by the compiler. If two units declare a variable, constant, type, procedure, or function with the same name, the compiler uses the one from the unit listed last in the uses clause. (To access the identifier from the other unit, you would have to add a qualifier: UnitName.Identifier.)

这是自 Turbo Pascal 4.0 引入单位以来的预期行为。

关于delphi - 为什么 Delphi 编译器不针对重新定义的常量发出警告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17293008/

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