I’m trying to create a simple class THistory that has one procedure that takes an abstract base class which implements a simple interface.
The code below compiles, but the THistory class calls the base class HistoryRecords abstract Insert proc instead of the passed in sub classes Insert proc. What am I missing?
Thanks for your help!
unit uHistory;
interface
uses Dialogs;
type
IHistoryRecord = interface
['{67C90064-1667-4DE0-AF52-11B6E5A00892}']
procedure Insert();
end;
THistoryRecord = class abstract(TInterfacedObject, IHistoryRecord)
procedure Insert(); virtual; abstract;
end;
THistory = class(TObject)
public
procedure Add(pHistoryRecord : THistoryRecord);
end;
TAlarmHistoryRecord = class(THistoryRecord)
procedure Insert();
end;
implementation
{ THistory }
procedure THistory.Add(pHistoryRecord: THistoryRecord);
begin
pHistoryRecord.Insert();
end;
{ TAlarmHistoryRecord }
procedure TAlarmHistoryRecord.Insert;
begin
MessageDlg('Alarm History Record - Insert Method', mtInformation, [mbOK], 0);
end;
end.
Usage
procedure TForm1.Button1Click(Sender: TObject);
var
lHistory : THistory;
lHistoryRecord : TAlarmHistoryRecord;
begin
lHistory := THistory.Create();
lHistoryRecord := TAlarmHistoryRecord.Create();
// I want this to call the TAlarmsHistoryRecord.Insert proc not the
// HistoryRecord base class Insert proc.
lHistory.Add(lHistoryRecord);
end;
You’re missing the
overridedirective in theTAlarmHistoryRecordmethod declaration, ie it should beActually the compiler should warn you that the method hides inherited one.