Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
776 views
in Technique[技术] by (71.8m points)

delphi - Interfaces and overload directive

The following code throws me the compiler error

E2252 Method 'MyFunction' with identical parameters already exists

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.

I understand there are two variables Integer type for each instance of the function but in one function both variables are both const and in the other function one variable one is const and the other one is var.

Why isn't that sufficient not to be considered identical parameters?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Because the overload resolution decision is taken by considering the calling code and not the declaration.

Suppose you call the function like this:

MyFunction(int1, int2);

Which one do you expect to be called? The const overload or the var overload? The compiler has no means to take that decision. Hence this is deemed ambiguous.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...