Need I to put overload or override words after the constructor declaration in derived class?

前端 未结 4 1010
不思量自难忘°
不思量自难忘° 2021-02-09 03:03

I have a class hierarchy, this one:

type
TMatrix = class
    protected
      //...
    public
      constructor Create(Rows, Cols: Byte);
    //...
type
  TMinMa         


        
4条回答
  •  暖寄归人
    2021-02-09 03:55

    You need overload for both constructors if they have the same name.

    type
      TMatrix = class
      protected
        //...
      public
        constructor Create(Rows, Cols: Byte);
        //...
    type
      TMinMatrix = class(TMatrix)
      public
        constructor Create(Rows, Cols: Byte); overload;
        constructor Create(var that: TMinMatrix); overload;
      end;
    

    It is good practice to call the inherited constructor.

    constructor TMinMatrix.Create(Rows, Cols: Byte);
    begin
       inherited Create(Rows, Cols); // Need to call the full name if the parameters are changed.
       //...
    end;
    

提交回复
热议问题