Delphi call inherited with overridden procedures if no explicit call

Does the Delphi call get inherited with overridden procedures if there is no explicit call in the code, that is (inherited;), I have the following structure (from super to subclass)

TForm β†’ TBaseForm β†’ TAnyOtherForm

All forms in the project will be obtained from TBaseForm, as this will have all the standard installation and destructive parts that are used for each form (security, validation ect).

TBaseForm has onCreate and onDestroy procedures with code to do this, but if someone (i.e. I) forgot to add the inherited in onCreate to TAnyOtherForm, will Delphi call it for me? I found links on the Internet that say it is not required, but it doesn’t say anywhere if it is called, if it is not specified in the code.

Also, if he calls the inherited one for me, when will he call him?

+7
inheritance oop delphi
source share
7 answers

No, if you leave the call inherited, it will not be called. Otherwise, it would be impossible to override the method and completely omit the parent version.

+17
source share

The inherited call must be explicitly specified. In general, a language does not automatically name an inherited function in equivalent situations (class constructors are not included).

It's easy to forget to make an inherited call in the class constructor. In such a situation, if the base class needs to initialize any data, you have an access violation awaiting its existence.

Perhaps you could override DoCreate and DoDestory in your TBaseForm class so that you can enforce any code regardless of the implementation of the child classes.

// interface TBaseForm = Class(TForm) ... Protected Procedure DoCreate(Sender : TObject); Override; End // implementation Procedure TBaseForm.DoCreate(Sender : TObject); Begin // do work here // let parent call the OnCreate property Inherited DoCreate(Sender); End; 
+3
source share

It should be noted that not calling any object inherited in Destroy can lead to memory leaks. There are tools in the source code to verify this.

+3
source share

Inherited must be explicitly invoked in descendant objects, as well as in the inheritance of the visual form. If you use a class, it adds the inherited one automatically if you are marked as overriding (but not for re-entry). If you use visual form inheritance, then when you add a new event handler through the form editor, it also adds the inherited one.

+2
source share

Inherited code is not called implicitly, as others have pointed out. You must call it explicitly. This gives you some useful flexibility. For example, you might want to make preprocessing code before inherited code, and then make code after processing. It might look like this:

 procedure TMyCalcObject.SolveForX; begin ResetCalcState; inherited SolveForX; PostProcessSolveForX; end; 
+1
source share

Not. That everything indicates redefinition.

0
source share

You must call this explicitly. This allows you to use more flexibility, because you can choose at what point in the code to call the inherited method. But it is also a big source of errors. It is easy to forget to call an inherited function, and the compiler cannot say whether you did it intentionally or simply forgot.

There must be some kind of "skip_inherited" directive to tell the compiler that you do not want to call the inherited method.

The compiler will then easily report an error if it finds neither "inherited" nor "skip_inherited". That would mean you forgot. But unfortunately, no one at CodeGear thought about this.

0
source share

All Articles