Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Base Class Inheritence Question

Status
Not open for further replies.

PaulField1

Programmer
Aug 13, 2008
1
GB
I have three classes A, B, C each inherriting from one another B=Class(A), C=Class(B). I have a virtual operation AddXYToSeries in each class. Class B adds some additional functionality then calls inherited AddXYToSeries. Class C adds some further functionality but needs to call Class A AddXYToSeries operation not the inherited Class B AddXYToSeries. Does anyone know the correct syntax to do this? In C++ (ahh!) you just call A::AddXYToSeries from within C::AddXYToSeries member function to get the appropriate one.

I've tried casting Self back to the base clase
A(Self).AddXYToSeries and (Self as A).AddXYToSeries but it just calls itself not the class A operation and I get a stack overflow.
I've also tried
inherited A.AddXYToSeries;
but the compiler did not like it.
 
I think this might be the way to access the inherited method:
inherited addxytoseries
 
Sorry about previous post, I misread your last lines.
I have not tried reaching back through several generations and am not sure how to skip the B level.
 
First, have a look here: thread102-1431491

I got what you described working with:
Code:
unit testvirtual;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    RunABtn: TButton;
    RunBBtn: TButton;
    RunCBtn: TButton;
    procedure RunABtnClick(Sender: TObject);
    procedure RunBBtnClick(Sender: TObject);
    procedure RunCBtnClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

  TA = class
    procedure AddXYToSeries; virtual; //abstract;
  private
    { Private declarations }
  public
    { Public declarations }
  end;

  TB = class(TA)
    procedure AddXYToSeries; override;
  private
    { Private declarations }
  public
    { Public declarations }
  end;

  TC = class(TA)
    procedure AddXYToSeries; override;
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TA.AddXYToSeries;
begin
  showMessage('Run from A')
end;

procedure TB.AddXYToSeries;
//Class B adds some additional functionality
//then calls inherited AddXYToSeries.
begin
  showMessage('Run from B');
  Inherited;
end;

procedure TC.AddXYToSeries;
//Class C adds some further functionality but needs
//to call Class A AddXYToSeries operation ...
//... NOT the inherited Class B AddXYToSeries.
begin
  showMessage('Run from C');
  Inherited;
end;

procedure TForm1.RunABtnClick(Sender: TObject);
var a: ta;
begin
  a:= ta.Create;
  a.AddXYToSeries;
  a.Free
end;

procedure TForm1.RunBBtnClick(Sender: TObject);
var b: tb;
begin
  b:= tb.Create;
  b.AddXYToSeries;
  b.Free
end;

procedure TForm1.RunCBtnClick(Sender: TObject);
var c: tc;
begin
  c:= tc.Create;
  c.AddXYToSeries;
  c.Free
end;

end.
Note the only variation in your objective:
B=Class(A), C=Class(B)
is:
B=Class(A), C=Class(A)

But it does what you asked for.
If C MUST inherit from B, then there's more work to do. Hopefully it's start.

Roo
Delphi Rules!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top