Message handler method in Delphi ignores that it is "private"

Viewed 98

There are two classes: TBaseClass and TDerivedClass, both in separate units.

TBaseClass

unit InhPriv.BaseClass;

interface

uses Vcl.Controls, Winapi.Windows, Winapi.Messages, InhPriv.Messages;

type
  TBaseClass = class(TWinControl)
  strict private
    procedure TheMethod(var msg: TMessage); message WM_CUSTOM;
  end;

implementation

procedure TBaseClass.TheMethod(var msg: TMessage);
begin
  MessageBox(0, 'TBaseClass.TheMethod', 'IhheritedPrivateTest', 0);
end;

end.

TDerivedClass

unit InhPriv.DerivedClass;

interface

uses InhPriv.BaseClass, Winapi.Windows, Winapi.Messages, InhPriv.Messages;

type
  TDerivedClass = class(TBaseClass)
  public
    procedure TheMethod(var msg: TMessage); message WM_CUSTOM;
  end;

implementation

procedure TDerivedClass.TheMethod(var msg: TMessage);
begin
  inherited; //TBaseClass.TheMethod gets called!!!
  MessageBox(0, 'TDerivedClass.TheMethod', 'IhheritedPrivateTest', 0);
end;

end.

Host form

TForm1 = class(TForm)
//.......
private
  c: TDerivedClass;
//.......
end;

WM_CUSTOM is defined as WM_USER + 1

Calling the method

Variant 1: Sending a message:

procedure TForm1.Button1Click(Sender: TObject);
begin
  SendMessage(c.Handle, WM_CUSTOM, 0, 0);
end;

Output:

TBaseClass.TheMethod
TDerivedClass.TheMethod

Variant 2: Calling the method directly:

procedure TForm1.Button1Click(Sender: TObject);
var m: TMessage;
begin
  c.TheMethod(m);
end;

Output:

TBaseClass.TheMethod
TDerivedClass.TheMethod

CPU window

It is a direct method call.

InhPriv.DerivedClass.pas.17: inherited; //TBaseClass.TheMethod gets called!!!
005B4880 8B55F8           mov edx,[ebp-$08]
005B4883 8B45FC           mov eax,[ebp-$04]
005B4886 E87DFDFFFF       call TBaseClass.TheMethod

Delphi documentation says:

When inherited has no identifier after it, it refers to the inherited method with the same name as the enclosing method or, if the enclosing method is a message handler, to the inherited message handler for the same message.

So, no explicit word says that converting a method to a message handler makes Delphi compiler violate OOP principles.

May I have missed some documentation?

0 Answers
Related