Solved

Virtual inheritence (I guess)

Posted on 2001-07-09
9
184 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:sorentop
ID: 6264493
No I have tried it with override, but it didn't seem to help
0
 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

803 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