加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

delphi – 为什么不在不同的单元中调用具有相同签名的函数会导致

发布时间:2020-12-15 09:49:35 所属栏目:大数据 来源:网络整理
导读:为什么这段代码不会导致编译错误?我本来期望错误,例如’对“CallMe”的模糊调用.这是编译器或语言中的错误吗?这可以通过使用单元名称和函数调用前面的点来解决,但这不会屏蔽用户代码和库代码以防止名称冲突.你认为你的代码做了一些事情,但它做了别的事情,
为什么这段代码不会导致编译错误?我本来期望错误,例如’对“CallMe”的模糊调用.这是编译器或语言中的错误吗?这可以通过使用单元名称和函数调用前面的点来解决,但这不会屏蔽用户代码和库代码以防止名称冲突.你认为你的代码做了一些事情,但它做了别的事情,这很糟糕.

uses
  Unit2,Unit3;

{$R *.lfm}
{ TForm1 }
procedure TForm1.Button1Click(Sender: TObject);
begin
  ShowMessage(IntToStr(CallMe(5)));
end;

unit Unit2;
{$mode objfpc}{$H+}
interface
uses
  Classes,SysUtils;
function CallMe(A: Integer) : Integer;
implementation
function CallMe(A: Integer) : Integer;
begin
  Result := A * 2;
end;
end.

unit Unit3;
{$mode objfpc}{$H+}
interface
uses
  Classes,SysUtils;
function CallMe(A: Integer) : Integer;
implementation
function CallMe(A: Integer) : Integer;
begin
  Result := A * -1;
end;
end.

解决方法

从 documentation开始:

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.)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读