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.
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:
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’ constructorvirtual, 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: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:
I hope this makes things more clear… Good luck!