views:

1327

answers:

4

I have a class hierarchy, this one:

type
TMatrix = class
    protected
      //...
    public
      constructor Create(Rows, Cols: Byte);
    //...
type
  TMinMatrix = class(TMatrix)
    private
      procedure Allocate;
      procedure DeAllocate;
    public
      constructor Create(Rows, Cols: Byte);
      constructor CreateCopy(var that: TMinMatrix);
      destructor Destroy;
  end;

So as you see, both derived and base class constructors have the same parameter list. I explicitly call base class constructor from derived one:

constructor TMinMatrix.Create(Rows, Cols: Byte);
begin
   inherited;
   //...
end;

Is it necessary to explicitly call base class constructor in Delphi? May be I need to put overload or override to clear what I intend to do? I know how to do it in C++ - you need explicit call of a base class constructor only if you want to pass some parameters to it - but I haven`t much experience in Delphi programming.

+2  A: 

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;
Gamecat
may be it is:<br><code>inherited Create(Rows, Cols)</code>?<br>Or parameter list doesn`t matter if the list is the same in both derived and base classes?
chester89
If the parameter lists are the same, you can leave them out. You can even leave out the *name* of the inherited constructor, and just say "inherited" instead of "inherited Create".
onnodb
+8  A: 

As far as I know, there are two separate issues here:

Making sure the child class' constructor calls the base class' constructor

You'll have to explicitly call the base class' constructor:

constructor TMinMatrix.Create(Rows, Cols: Byte);
begin
   inherited;
   //...
end;

Making sure the child class' constructor overrides the base class' constructor

You'll also have to make the child class' constructor override, and the base class' constructor virtual, to make sure the compiler sees the relation between the two. If you don't do that, the compiler will probably warn you that TMinMatrix's constructor is "hiding" TMatrix's constructor. So, the correct code would be:

type
TMatrix = class
    protected
      //...
    public
      constructor Create(Rows, Cols: Byte); virtual;    // <-- Added "virtual" here
      //...
type
  TMinMatrix = class(TMatrix)
    private
      //...
    public
      constructor Create(Rows, Cols: Byte); override;   // <-- Added "override" here
      constructor CreateCopy(var that: TMinMatrix);
      destructor Destroy; override;                     // <-- Also make the destructor "override"!
  end;

Note that you should also make your destructor override.

Introducing a constructor with different parameters

Note that you can only override a constructor with the same parameter list. If a child class needs a constructor with different parameters, and you want to prevent the base class' constructors from being called directly, you should write:

type
TMyMatrix = class(TMatrix)
//...
public
  constructor Create(Rows, Cols, InitialValue: Byte); reintroduce; virtual;
//...
end

implementation

constructor TMyMatrix.Create(Rows, Cols, InitialValue: Byte);
begin
  inherited Create(Rows, Cols);   // <-- Explicitly give parameters here
  //...
end;

I hope this makes things more clear... Good luck!

onnodb
Correction: Normal constrcutors and not virtual, you don't need to use override on their descendants and you won't get compiler warnings about not doing so.The only (common) case of using virtual contsructors (with override) is for descendants of TComponent.
Mike Sutton
Oh, is it? Hm, I must've missed something along the way then... So, for "normal" (non-TComponent) classes, you can leave out the virtual/override stuff and still call inherited, and the whole thing'll still work?
onnodb
+2  A: 

Overload, tells the compiler that a method has the same name with different parameters.

Override, tells the compiler that a method overrides it virtual or dynamic declared in the base class.

Reintroduce, will hide the virtual or dynamic method declared in the base class.

Theses definitions are from Ray Lischner's book {Delphi in a nutshell}

type
  TFirst = class
  private
    FValue: string;
    FNumber: Integer;
  public
    constructor Create(AValue: string; ANumber: integer);

    property MyValue: string read FValue write FValue;
    property MyNumber: Integer read Fnumber write FNumber; 
  end;

  TSecond = class(TFirst)
  public
    constructor Create(AValue: string; ANumber: Integer);
  end;

constructor TFirst.Create(AValue: string; ANumber: integer);
begin
  MyValue := AValue;
  MyNumber := ANumber;
end;

{ TSecond }

constructor TSecond.Create(AValue: string; ANumber: Integer);
begin
  inherited;
end;

The TSecond as it is declared will call the create of the TFirst, without the inherited, the TSecond members stay empty.

CheGueVerra
+1  A: 

You need to call the inherited method explicitly; Delphi won't do it for you. This is by design, because there are cases where you're working with virtual methods, for example, when you do not want to invoke the inherited behavior.

Also, as a matter of personal preference, I like to write out the inherited call completely. (inherited Create(Rows, Cols); as opposed to just inherited; for one simple reason: it makes it that much easier to traverse the code. If you've got a method call written out, you can control-click it and get to the ancestor method.

Mason Wheeler