i'm looking to understand
- virtual
- override
- overload
- reintroduce
when applied to object constructors. Every time i randomly add keywords until the compiler shuts up - and (after 12 years of developing with Delphi) i'd rather know what i'm doing, rather than trying things randomly.
Given a hypothetical set of objects:
TComputer = class(TObject)
public
constructor Create(Cup: Integer); virtual;
end;
TCellPhone = class(TComputer)
public
constructor Create(Cup: Integer; Teapot: string); virtual;
end;
TiPhone = class(TCellPhone)
public
constructor Create(Cup: Integer); override;
constructor Create(Cup: Integer; Teapot: string); override;
end;
The way i want them to behave is probably obvious from the declarations, but:
TComputer
has the simple constructor, and descendants can override itTCellPhone
has an alternate constructor, and descendants can override itTiPhone
overrides both constructors, calling the inherited version of each
Now that code doesn't compile. i want to understand why it doesn't work. i also want to understand the proper way to override constructors. Or perhaps you could never override constructors? Or perhaps it is perfectly acceptable to override constructors? Perhaps you should never have multiple constructors, perhaps it is perfectly acceptable to have multiple constructors.
i want to understand the why. Fixing it would then be obvious.
See also
- Delphi: How to hide ancestor constructors?
- Reintroducing functions in Delphi
- Delphi: How to add a different constructor to a descendant?
Edit: i'm also looking to get some reasoning on the order of virtual
, override
, overload
, reintroduce
. Because when trying all combinations of keywords, the number of combinations explodes:
- virtual; overload;
- virtual; override;
- override; overload;
- override; virtual;
- virtual; override; overload;
- virtual; overload; override;
- overload; virtual; override;
- override; virtual; overload;
- override; overload; virtual;
- overload; override; virtual;
- etc
Edit 2: i guess we should begin with "is the object hierarchy given even possible?" If not, why not? For example, is it fundamentally incorrect to have a constructor from an ancestor?
TComputer = class(TObject)
public
constructor Create(Cup: Integer); virtual;
end;
TCellPhone = class(TComputer)
public
constructor Create(Cup: Integer; Teapot: string); virtual;
end;
i would expect that TCellPhone
now has two constructors. But i can't find the combination of keywords in Delphi to make it think that's a valid thing to do. Am i fundamentally wrong in thinking i can have two constructors here in TCellPhone
?
Note: Everything below this line is not strictly needed to answer the question - but it does help to explain my thinking. Perhaps you can see, based on my thought processes, what fundamental piece i'm missing that makes everything clear.
Now these declarations don't compile:
//Method Create hides virtual method of base type TComputer:
TCellPhone = class(TComputer)
constructor Create(Cup: Integer; Teapot: string); virtual;
//Method Create hides virtual method of base type TCellPhone:
TiPhone = class(TCellPhone)
public
constructor Create(Cup: Integer); override;
constructor Create(Cup: Integer; Teapot: string); overload; <--------
end;
So first i'll trying fixing TCellPhone
. i'll start by randomly adding the overload
keyword (i know i don't want reintroduce
because that would hide the other constructor, which i don't want):
TCellPhone = class(TComputer)
public
constructor Create(Cup: Integer; Teapot: string); virtual; overload;
end;
But that fails: Field definition not allowed after methods or properties
.
i know from experience that, even though i don't have a field after a method or property, if i reverse the order of the virtual
and overload
keywords: Delphi will shut up:
TCellPhone = class(TComputer)
public
constructor Create(Cup: Integer; Teapot: string); overload; virtual;
end;
But i still get the error:
Method 'Create' hides virtual method of base type 'TComputer'
So i try removing both keywords:
TCellPhone = class(TComputer)
public
constructor Create(Cup: Integer; Teapot: string);
end;
But i still get the error:
Method 'Create' hides virtual method of base type 'TComputer'
So i resign myself to now trying reintroduce
:
TCellPhone = class(TComputer)
public
constructor Create(Cup: Integer; Teapot: string); reintroduce;
end;
And now TCellPhone compiles, but it has made things much worse for TiPhone:
TiPhone = class(TCellPhone)
public
constructor Create(Cup: Integer); override; <-----cannot override a static method
constructor Create(Cup: Integer; Teapot: string); override; <-----cannot override a static method
end;
Both are complaining that i cannot override them, so i remove the override
keyword:
TiPhone = class(TCellPhone)
public
constructor Create(Cup: Integer);
constructor Create(Cup: Integer; Teapot: string);
end;
But now the 2nd create says it must be marked with overload, which i do (in fact i'll mark both as overload, since i know what will happen if i don't):
TiPhone = class(TCellPhone)
public
constructor Create(Cup: Integer); overload;
constructor Create(Cup: Integer; Teapot: string); overload;
end;
All all is good in the interface
section. Unfortunately my implementations won't work. My single parameter constructor of TiPhone cannot call the inherited constructor:
constructor TiPhone.Create(Cup: Integer);
begin
inherited Create(Cup); <---- Not enough actual parameters
end;
reintroduce
;overload
; binding; calling convention;abstract
; warning where binding isvirtual
,dynamic
, oroverride
; calling convention isregister
,pascal
,cdecl
,stdcall
, orsafecall
; and warning isplatform
,deprecated
, orlibrary
. – Muhammad Alkarouri