Inherited/non-inherited constructor and destructor
Closed this issue · 2 comments
In the example:
uses SysUtils;
type
TGun = class
end;
TPlayer = class
Gun1, Gun2: TGun;
constructor Create;
destructor Destroy; override;
end;
constructor TPlayer.Create;
begin
inherited;
Gun1 := TGun.Create;
Gun2 := TGun.Create;
end;
destructor TPlayer.Destroy;
begin
FreeAndNil(Gun1);
FreeAndNil(Gun2);
inherited;
end;
We don't have override for constructor Create; but still we call inherited; - is this correct? Maybe an additional note would be nice on why destructor must be override and must have inherited; but the constructor doesn't seem to need those?
The example is correct.
And it's a good question, I can see that it may be unclear. Thank you! I'll think how to nicely express it in the book. For now, a quick reply:
"inherited" means that you call a method of an ancestor. If doesn't matter whether it's a virtual method or not.
"override" is only useful when you override a virtual method of an ancestor. When an ancestor has a virtual method Foo, and you define a method also called Foo, you must mark it with either "override" or "reintroduce". In 99% cases you want to use "override", because it means that virtual methods work like they should (calling TAncestor.Foo will dynamically call either the original, or overridden Foo, based on the class instance).
The "Destroy" is virtual at TObject, so you always want to override it in descendants. This way the Free method, and FreeAndNil procedure, work (as they call something like TObject(MyInstance).Destroy inside).
In contrast, there is no virtual constructor in TObject. So you have nothing to override.
Note that this is true because TPlayer descends from TObject (class without any ancestor is equivalent to class(TObject)). If the TPlayer would descend from TComponent, then the situation would be different, as TComponent has a virtual constructor Create(AOwner: TComponent), and usually you want to override it in descendants.
To explain this correctly, this topic deserved a three new sections in the book :)
- 4.7. Calling inherited method, http://michalis.ii.uni.wroc.pl/~michalis/modern_pascal_introduction/modern_pascal_introduction.html#_calling_inherited_method
- 4.8. Virtual methods, override and reintroduce, http://michalis.ii.uni.wroc.pl/~michalis/modern_pascal_introduction/modern_pascal_introduction.html#virtual-methods-section
- 5.4. The virtual destructor called Destroy, http://michalis.ii.uni.wroc.pl/~michalis/modern_pascal_introduction/modern_pascal_introduction.html#_the_virtual_destructor_called_destroy
I hope that these are informative :)