如何根据结果类型重载函数?

发布于 2024-12-04 20:22:52 字数 780 浏览 0 评论 0原文

只是一个问题,我有:

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

它让我出错,所以我尝试过:

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

但同样的问题; delphi告诉我有相同的参数。 现在,我问,是否可以在模式下执行更多具有相同名称但具有不同输出的功能,如示例所示? 非常感谢您的帮助。

更新

抱歉,我犯了一个错误,不是 funct1 和 funct2,而是 funct1,所以:

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

这样做,编译器返回此错误:

[DCC Error] Project1.dpr(15): E2252 具有相同参数的方法“funct1”已存在 [DCC 错误] Project1.dpr(22): E2037 'funct1' 声明与之前的声明不同

当然,我知道因为给出错误并且需要将名称更改为两个函数之一(因为我之前很困惑),但我想知道是否有一些技巧或其他解决方案可以让这种情况没有错误。 再次感谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

酒废 2024-12-11 20:22:53

您可以将该函数转换为带有 var 参数的过程:

myclass = class
public
  procedure Funct1(var AResult: String); overload;
  procedure Funct1(var AResult: Integer); overload;
end;

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;
爱要勇敢去追 2024-12-11 20:22:53

你发的帖子没有意义。第一个示例编译应该没有任何问题,因为函数具有不同的名称Funct1Funct2

仅当方法(或函数)具有相同名称时才会出现问题。那么,通常情况下,overload 指令就可以了,但是 overload 无法仅根据返回值区分函数

因此,假设名称相同,您想要的就是不可能的。如果这些函数没有不同的参数签名,则无法重载它们。您可以给它们不同的名称,无论如何,这是更好的选择,因为它们显然做不同的事情。


FWIW,您的问题是有缺陷的,因为您显然没有发布您实际遇到问题的确切代码。请始终发布导致问题的确切代码,如果有错误消息,请始终发布确切的错误消息(通常可以使用常用的复制按键来复制它们,例如 Ctrl+C,即使在 IDE 的大部分部分或在Delphi 中的消息对话框)。如果错误消息中有任何行号,请在您发布的源代码中指出这一点,因为我们并不总是具有与您相同的行号。

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.

浅黛梨妆こ 2024-12-11 20:22:53

首先,要重载函数,它们必须命名相同。

其次,这是不可能做到的。重载函数必须具有不同的参数。

在您的情况下,编译器无法判断要调用哪个函数(假设两者都重命名为 Funct1):

var
  v: Variant;
  mc: myclass;
begin
  v := mc.Funct1;
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;
何以心动 2024-12-11 20:22:53

如果您想重载具有不同返回类型的方法,只需添加一个具有默认值的虚拟参数即可允许重载执行,但不要忘记参数类型应该不同,以便重载逻辑起作用,例如:

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

像这样使用它

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;

也不要忘记,这样你就会遇到变体问题,如下例所示:

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

直到你也实现了变体函数:

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

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;
北方的韩爷 2024-12-11 20:22:53

正如已经说过的,你不能按照你想做的那样去做。但是,您可以使用不同的名称(例如“AsInteger”或“AsString”)轻松实现每个函数。您的代码会更清晰,这通常是在 VCL 中完成的方式。

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

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文