【问题标题】:Interfaces and overload directive接口和重载指令
【发布时间】:2021-04-14 09:58:29
【问题描述】:

以下代码向我抛出编译器错误

具有相同参数的 E2252 方法“MyFunction”已存在

program Project3;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils;

type
  aMyInterface = interface
    function MyFunction(const aSort: Integer; var aEndPoint: Integer): Integer; overload;
    function MyFunction(const aSort, aStartingPoint: Integer): Integer; overload;
  end;

  aMyClass = class(TInterfacedObject, aMyInterface)
    function MyFunction(const aSort: Integer; var aEndPoint: Integer): Integer; overload;
    function MyFunction(const aSort, aStartingPoint: Integer): Integer; overload;
  end;

{ aMyClass }

function aMyClass.MyFunction(const aSort: Integer; var aEndPoint: Integer): Integer;
begin
  Result := 1;
end;

function aMyClass.MyFunction(const aSort, aStartingPoint: Integer): Integer;
begin
  Result := 1;
end;

begin
end.

我知道函数的每个实例都有两个变量整数类型,但在一个函数中,两个变量都是 const,而在另一个函数中,一个变量是 const另一个是 var

为什么不能将其视为相同的参数?

【问题讨论】:

    标签: delphi


    【解决方案1】:

    因为重载决策是通过考虑调用代码而不是声明来做出的。

    假设你这样调用函数:

    MyFunction(int1, int2);
    

    你希望被叫到哪一个? const 重载还是 var 重载?编译器无法做出该决定。因此,这被认为是模棱两可的。

    【讨论】:

    • 我从来没有考虑过从调用代码编译器的角度来看。我今天学到了一些非常新的东西。
    猜你喜欢
    • 2011-12-14
    • 1970-01-01
    • 2014-09-21
    • 2022-01-23
    • 2020-01-15
    • 2013-03-16
    • 2020-07-22
    • 2018-01-20
    • 2021-10-12
    相关资源
    最近更新 更多