开发者

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 want开发者_Go百科ed know if there was some trick or other solution for to have a situation as this without error. Thanks again.


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;


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;


What you post doesn't make sense. The first example should compile without any problem, as the functions have different names, Funct1 and Funct2.

Problems only arise when methods (or functions) have the same name. Then, normally, an overload directive would be in order, but overload can't distinguish functions on return value alone.

So assuming the names are the same, what you want is impossible. There is no way to overload these functions, if they don't have a different parameter signature. You can just give them different names, which is preferrable anyway, as they apparently do different things.


FWIW, your question is flawed by the fact that you apparently did not post the exact code with which you are actually having problems. Please always post the exact code that causes your problems, and if there are error messages, always post the exact error message (they can usually be copied using the usual copy keystrokes, e.g. Ctrl+C, even in most parts of the IDE or in message dialogs in Delphi). If there are any line numbers in the error message, indicate this in the source code you post, as we don't always have the same line numbers as you have.


if you want to overload methods with different return types, just add a dummy parameter with default value to allow the overload execution, but don't forget the parameter type should be different so the overload logic works e.g:

type    
    myclass = class
    public
      function Funct1(dummy: string = EmptyStr): String; overload;
      function Funct1(dummy: Integer = -1): Integer; overload;
    end;

use it like this

procedure tester;
var yourobject : myclass;
  iValue: integer;
  sValue: string;
begin
  yourobject:= myclass.create;
  iValue:= yourobject.Funct1(); //this will call the func with integer result
  sValue:= yourobject.Funct1(); //this will call the func with string result
end;

also don't forget that this way your gonna have a problem with variants like in this example:

procedure tester;
var yourobject : myclass;
  vValue: variant;
begin
  yourobject:= myclass.create;
  vValue:= yourobject.Funct1(); 
end;

until you implement the variant function too:

      function Funct1(dummy: Variant = Unassigned): Variant; overload;


As was already stated, you cannot do it as you are wanting to do. However, you could just as easily implement each function with a different name such as "AsInteger" or "AsString". Your code will be clearer and this is generally the way it is done within the VCL.

TmyClass = class (TObject)
public
  function AsString : string;
  function AsInteger : Integer;
end;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜