sorentop
asked on
Virtual inheritence (I guess)
I have two form which must have the same public procedure, so I decide to create an abstract base class for them to inheritence like this.
TAbstractForm = class(TForm)
public
function ShowX: integer; virtual; abstract;
end;
The two subclass forms look like this
TFormX = class(TAbstractForm)
public
function ShowX: integer; virtual;
end;
function TFormX.ShowX: integer;
begin
ShowModal;
result := 217;
end;
I then want to call a procedure with either of the two forms like this
procedure TForm1.ShowMyForm( Form: TAbstractForm );
begin
Form.ShowX;
end;
the actual call
..
begin
ShowMyForm( FormX );
end;
I get this strange memory error when performing the last call. I guess because Delphi tries to execute ShowX of the abstract base class.
How do I handle this problem ?
BTW this was just a quick example I put together, so perhaps some details is forgotten or left out.
TAbstractForm = class(TForm)
public
function ShowX: integer; virtual; abstract;
end;
The two subclass forms look like this
TFormX = class(TAbstractForm)
public
function ShowX: integer; virtual;
end;
function TFormX.ShowX: integer;
begin
ShowModal;
result := 217;
end;
I then want to call a procedure with either of the two forms like this
procedure TForm1.ShowMyForm( Form: TAbstractForm );
begin
Form.ShowX;
end;
the actual call
..
begin
ShowMyForm( FormX );
end;
I get this strange memory error when performing the last call. I guess because Delphi tries to execute ShowX of the abstract base class.
How do I handle this problem ?
BTW this was just a quick example I put together, so perhaps some details is forgotten or left out.
Hi sorentop!
Just first look but,
instead
TFormX = class(TAbstractForm)
public
function ShowX: integer; virtual;
end;
try
TFormX = class(TAbstractForm)
public
function ShowX: integer; override;
end;
Hope this helps, Ivo
Just first look but,
instead
TFormX = class(TAbstractForm)
public
function ShowX: integer; virtual;
end;
try
TFormX = class(TAbstractForm)
public
function ShowX: integer; override;
end;
Hope this helps, Ivo
Oh, damn! Only 2 minutes delay.
Ivo
Ivo
ASKER
No I have tried it with override, but it didn't seem to help
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
FormX is a form just like when you have Delphi create a default one. With the var in the interface section.
Instead of TForm1 = class(TForm) I have changed it to TFormX = class(TAbastractForm)
FormX is also created automatic by Delphi
Instead of TForm1 = class(TForm) I have changed it to TFormX = class(TAbastractForm)
FormX is also created automatic by Delphi
ASKER
The creation look like this
Application.CreateForm(TFo rmX, FormX);
Application.CreateForm(TFo
ASKER
Wait a minute. I have found the error, I turned out I assigned the variable of FormX in the Form1's OnCreation, before! FormX was created.
OK. I have also tried to run the project and it worked well. But remember to always OVERRIDE the virtual (abstract) method otherwise when you declare the method with same name with virtual directive in descendant class, you simply declare another method (and hide ancestor's one) and in case the original method was abstract, you get EAbstractError exception when attempting to call an abstract method.
Best regards, Ivo.
Best regards, Ivo.
TFormX = class(TAbstractForm)
public
function ShowX: integer; virtual;
end;
must be
TFormX = class(TAbstractForm)
public
function ShowX: integer; override;
end;