Solved

Virtual inheritence (I guess)

Posted on 2001-07-09
9
182 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
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…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

862 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

23 Experts available now in Live!

Get 1:1 Help Now