Delphi/pascal: overloading a constructor with a different prototype

半腔热情 提交于 2019-12-18 11:47:30

问题


I'm trying to create a child class of TForm with

  1. a special constructor for certain cases, and
  2. a default constructor that will maintain compatibility with current code.

This is the code I have now:

interface
  TfrmEndoscopistSearch = class(TForm)
  public
    /// original constructor kept for compatibility
    constructor Create(AOwner : TComponent); overload; override;
    /// additional constructor allows for a caller-defined base data set
    constructor Create(AOwner : TComponent; ADataSet : TDataSet; ACaption : string = ''); overload;
  end;

It seems to work, but I always get the compiler warning:

[Warning] test.pas(44): Method 'Create' hides virtual method of base type 'TCustomForm'
  • Adding "overload;" after the second constructor won't compile. "[Error] test.pas(44): Declaration of 'Create' differs from previous declaration".
  • making the second constructor a class function compiles without any errors or warnings, but dies with an access violation at runtime (all member vars are nil).

回答1:


Try adding reintroduce before the second overload, like this:

  TfrmEndoscopistSearch = class(TForm)
  public
    /// original constructor kept for compatibility
    constructor Create(AOwner : TComponent); overload; override;
    /// additional constructor allows for a caller-defined base data set
    constructor Create(AOwner : TComponent; ADataSet : TDataSet; ACaption : string = ''); reintroduce; overload;
  end;

This compiles in Turbo Delphi. I needed the public to make it compile because overloading of published methods is restricted.




回答2:


There's a really easy way to avoid this. Give your new constructor a different name. Unlike some other popular languages, Delphi has named constructors; you don't have to call them Create. You could call your new one CreateWithDataset and not interfere with the virtual Create constructor at all.

TfrmEndoscopistSearch = class(TForm)
  /// original constructor kept for compatibility
  constructor Create(AOwner: TComponent); override;
  /// additional constructor allows for a caller-defined base data set
  constructor CreateWithDataset(AOwner: TComponent; ADataSet: TDataSet; ACaption: string = '');
end;

In fact, unless you're instantiating this class polymorphically, you don't even need the original constructor. You could declare your new one like this:

TfrmEndoscopistSearch = class(TForm)
  /// additional constructor allows for a caller-defined base data set
  constructor Create(AOwner: TComponent; ADataSet: TDataSet; ACaption: string = ''); reintroduce;
end;

Attempting to call the one-argument constructor directly on TfrmEndoscopistSearch would yield a compilation error.


(Creating it polymorphically would generally involve using Application.CreateForm:

Application.CreateForm(TfrmEndoscopistSearch, frmEndoscopistSearch);

That always calls the one-argument virtual constructor introduced in TComponent. Unless it's your main form, you don't need to do that. I've written about my feelings on Application.CreateForm before.)




回答3:


constructor Create(AOwner:Tcomponent;str:string);overload;
... 
constructor TfrmEndoscopistSearch.Create(AOwner: Tcomponent; str: string);
    begin
    inherited Create(AOwner);
    showmessage(str);
    end;

This should do the trick



来源:https://stackoverflow.com/questions/1758917/delphi-pascal-overloading-a-constructor-with-a-different-prototype

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!