开发者

Delphi, is possible have a property and function with the same name?

consider this code

type
  TMyObject=class
  private
    FName: Integer;
    function Name: Integer;
  public
    property Name : Integer read FName;
    function Name(Param1 : integer) 开发者_开发问答: Integer; overload;
  end;

Is possible in delphi create a property and a function (or procedure) with the same name?

Exist any directive or compiler switch which allow create a class like that?


No, it is not. In addition, there is no directive or compiler switch that would allow this.


The answer is in the error message that you got when you tried this...you did try it right?


Since i see the overload keyword. I suspect that perhaps what you need default are parameters

If you use

function Name(Param1: Integer = SOME_VALUE): Integer;

it can be called as either :=Name or :=Name(5)


By accident I discovered it is possible to have a class with a property that has the same name as a function in the parent class (or vice versa).

However, I would avoid this because it will confuse you. Especially if the function and property have different meanings!


Currently, the compiler cannot do what you want.
In theory, a future version of the compiler could:

The signature of a method (overloaded or not) consists of the name and the parameter types.
The same holds for the signature of indexed properties.
Since the signature spaces of properties and methods are partially linked (hence the compiler error message), that combined space could be extended to include property overloads.

Of course that extension can backfire because of backward compatibility.

--jeroen

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜