SerialForms.pas(17): W1010 Method 'Create' hides virtual method of base type 'TComponent'

被刻印的时光 ゝ 提交于 2019-12-23 14:21:08

问题


I created a class

  FormInfo = class (TComponent)
  private
    FLeftValue : Integer;
    FTopValue : Integer;
    FHeightValue : Integer;
    FWidthValue : Integer;
  public
    constructor Create(
      AOwner : TComponent;
      leftvalue : integer;
      topvalue : integer;
      heightvalue : integer;
      widthvalue : integer);
  protected
    procedure GetChildren(Proc: TGetChildProc; Root: TComponent); override;
    function  GetChildOwner: TComponent; override;
    //procedure SetParentComponent(Value : TComponent); override;
  published
    property LeftValue : Integer read FLeftValue write FLeftValue;
    property TopValue : Integer read FTopValue write FTopValue;
    property HeightValue : Integer read FHeightValue write FHeightValue;
    property WidthValue : Integer read FWidthValue write FWidthValue;
  end;

which is used further for form serialization. The Create method has the following implementation

constructor FormInfo.Create(AOwner: TComponent; leftvalue, topvalue, heightvalue,
  widthvalue: integer);
begin
  inherited Create(AOwner);

  FLeftValue := leftvalue;
  FTopValue := topvalue;
  FHeightValue := heightvalue;
  FWidthValue := widthvalue;
end;

As a result of assembly I receive warning

[dcc32 Warning] SerialForms.pas(17): W1010 Method 'Create' hides virtual method of base type 'TComponent'

What it is necessary to make to get rid of this warning without loss of functionality of application?


回答1:


Use the reintroduce reserved word to indicate the compiler you want to intentionally hide the base class constructor in your class:

TMyClass = class (TComponent)
public
  constructor Create(AOwner: TComponent; MyParam: Integer; Other: Boolean); reintroduce;

That way, no warning is shown.

That said, you have to re-think hiding the TComponent.Create constructor. It is a bad idea since the default TComponent.Constructor is called by Delphi to create your component instances at run-time when added to forms/data modules at design time.

TComponent makes the constructor virtual to allow you execute custom code during that process, but you have to stick with the Create firm passing you only the owner, and let the streaming mechanism to deal with the stored values for properties after the creation is complete.

Your component have to support being "unconfigured" if that's the case, or setup default values for it's properties in this universal constructor.

You can provide more constructors, with different names, to allow you create instances at run-time from code passing values for the different properties for your convenience.




回答2:


It might be better - and more readable if you use a different name for your constructor, such as

constructor FormInfo.CreateWithSize(AOwner: TComponent; leftvalue, topvalue, heightvalue, widthvalue: integer);


来源:https://stackoverflow.com/questions/13954582/serialforms-pas17-w1010-method-create-hides-virtual-method-of-base-type-tc

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