Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to overload a function based on the result type?

just a question, i have:

myclass = class
public
  function Funct1: String;
  function Funct2: Integer;
end;

It turn me error, so i have tried with:

myclass = class
public
  function Funct1: String; overload;
  function Funct2: Integer; overload;
end;

but same problem; delphi tell me that has same parameter. Now, i ask, is possible to do in mode to have more function with same name but with different output as in example? Thanks very much for help.

UPDATE

Sorry, i done a error, not funct1 and funct2, but both funct1, so:

myclass = class
public
  function Funct1: String; overload;
  function Funct1: Integer; overload;
end;

Doing so, compiler return me this error:

[DCC Error] Project1.dpr(15): E2252 Method 'funct1' with identical parameters already exists [DCC Error] Project1.dpr(22): E2037 Declaration of 'funct1' differs from previous declaration

Of course, i know becouse give error and need change name to one of both function (for it me confused before) but i wanted know if there was some trick or other solution for to have a situation as this without error. Thanks again.

like image 667
Marcello Impastato Avatar asked Sep 15 '11 10:09

Marcello Impastato


People also ask

Can you overload a function based on return type?

The return type of a function has no effect on function overloading, therefore the same function signature with different return type will not be overloaded. Example: if there are two functions: int sum() and float sum(), these two will generate a compile-time error as function overloading is not possible here.

How do you overload a function?

You overload a function name f by declaring more than one function with the name f in the same scope. The declarations of f must differ from each other by the types and/or the number of arguments in the argument list.

What are the conditions to overload a function?

Rules of Function Overloading in C++The functions must have the same name. The functions must have different types of parameters. The functions must have a different set of parameters. The functions must have a different sequence of parameters.

Can we overload method with different return type C#?

The compiler does not consider the return type while differentiating the overloaded method. But you cannot declare two methods with the same signature and different return type. It will throw a compile-time error. If both methods have the same parameter types, but different return type, then it is not possible.


2 Answers

You could turn the function into a procedure taking a var parameter:

myclass = class
public
  procedure Funct1(var AResult: String); overload;
  procedure Funct1(var AResult: Integer); overload;
end;
like image 174
Uli Gerhardt Avatar answered Sep 28 '22 07:09

Uli Gerhardt


Firstly, for functions to be overloaded, they have to be named the same.

Secondly, this is not possible to do. Overloaded functions must have different parameters.

In your case, there is no way the compiler can tell which of your functions to call (assumed that both are renamed to Funct1):

var
  v: Variant;
  mc: myclass;
begin
  v := mc.Funct1;
end;
like image 39
gabr Avatar answered Sep 28 '22 06:09

gabr