Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inherited/non-inherited constructor and destructor #8

Closed
eugeneloza opened this issue Apr 21, 2017 · 2 comments
Closed

Inherited/non-inherited constructor and destructor #8

eugeneloza opened this issue Apr 21, 2017 · 2 comments

Comments

@eugeneloza
Copy link
Contributor

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?

@michaliskambi
Copy link
Owner

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.

michaliskambi added a commit that referenced this issue May 27, 2017
michaliskambi added a commit that referenced this issue May 27, 2017
michaliskambi added a commit that referenced this issue May 27, 2017
And how it's different from constructors

See #8
@michaliskambi
Copy link
Owner

To explain this correctly, this topic deserved a three new sections in the book :)

I hope that these are informative :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants