Solved

Virtual inheritence (I guess)

Posted on 2001-07-09
9
181 Views
Last Modified: 2010-08-05
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.
0
Comment
Question by:sorentop
  • 4
  • 4
9 Comments
 
LVL 13

Expert Comment

by:Epsylon
ID: 6264467
I think this

TFormX = class(TAbstractForm)
public
 function ShowX: integer; virtual;
end;

must be

TFormX = class(TAbstractForm)
public
 function ShowX: integer; override;
end;
0
 
LVL 2

Expert Comment

by:ivobauer
ID: 6264476
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
0
 
LVL 2

Expert Comment

by:ivobauer
ID: 6264491
Oh, damn! Only 2 minutes delay.

Ivo
0
 

Author Comment

by:sorentop
ID: 6264493
No I have tried it with override, but it didn't seem to help
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 2

Accepted Solution

by:
ivobauer earned 150 total points
ID: 6264512
sorentop:

> the actual call
>..
>begin
>  ShowMyForm( FormX );
>end;

Where does the FormX variable come from? Please post a code snippet where you get/create this var.

Ivo
0
 

Author Comment

by:sorentop
ID: 6264537
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
0
 

Author Comment

by:sorentop
ID: 6264548
The creation look like this
Application.CreateForm(TFormX, FormX);
0
 

Author Comment

by:sorentop
ID: 6264580
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.
0
 
LVL 2

Expert Comment

by:ivobauer
ID: 6264623
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.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now