Solved

Virtual inheritence (I guess)

Posted on 2001-07-09
9
185 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Controlled Assessment GCSE - desperate help needed 4 102
RESTRequest Parameter 4 43
Tidtcpserver listening on multiports? 1 35
scroll down TListBox component in Delphi 1 19
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

829 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