Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Calling inherited functions (as opposed to procedures)?

809 views
Skip to first unread message

Riaan Moll

unread,
May 30, 2008, 6:02:46 AM5/30/08
to
Hi all,

Here's my scenario: I would like to build a taxonomy where descendent
classes call the immediate parent-class's methods (functions,
specifically), take the return value and add to it. My problem stems
from what appears to be that calling inherited method functions do not
work as functions, but as procedures, in the sense that the inherited
function is still called, but you cannot retrieve the return value;

Example: (I've mixed the type declaration and method implementations
for clarity's sake)

//////////////////////////
type TAncestor = class
function GetAString : string; virtual;
end;

Function TAncestor.GetAString : String;
begin
Result := 'Borland';
end;

//////////////////////////
type TDesc1 = class(TAncestor)
function GetAString : String; override;
end;

Function TDesc1.GetAString : String;
begin
Result := inherited; // want to call TAncestor.GetAString but gives
compile time error ('Incompatible types');
Result := Result + 'Delphi';
end;

//////////////////////////
type TDesc2 = class(TDesc1)
function GetAString : String; override;
end;

Function TDesc2.GetAString : String;
begin
Result := inherited; // want to call TDesc1.GetAString but gives
compile time error ('Incompatible types');
Result := Result + 'Rocks!';
end;


The idea is to call the GetAString method of any instance of TAncestor
or its descendant classes, and each class would return its string
concatenated with the concatenated strings of all ancestors up the
tree. Thus calling TDesc2.GetAString must yield "BorlandDelphiRocks!".

But it ain't compiling. What am I missing? The documentation says that
one should avoid using the ClassParent method to reference the
immediate parent (and it would be somewhat clunky anyway, in my
opinion), so how do I go about doing this?

Thanks in advance!

Riaan

Riaan Moll

unread,
Jun 4, 2008, 3:13:29 AM6/4/08
to
Anybody?

djb...@yahoo.co.uk

unread,
Jul 31, 2008, 11:43:50 AM7/31/08
to
On Jun 4, 8:13 am, Riaan Moll <riaanm...@gmail.com> wrote:
> Anybody?

Try calling the inherited function by name, like so:

Function TDesc1.GetAString : String;
begin
Result := inherited GetAString;


Result := Result + 'Delphi';
end;

Function TDesc2.GetAString : String;
begin
Result := inherited GetAString;

0 new messages