tags:

views:

127

answers:

2

Hi.

I have a class TChild derived from TParent. TParent has a property MyProp that is reading and setting some values in an array. Of course this property is inherited by the TChild, but I want to add few extra processing in child's property. The code below explains better what I want to do but it is not working. How can I implement it?

TParent = class...
 private
   function  getStuff(index: integer): integer; virtual;
   procedure setStuff(index: integer; value: integer); virtual;
 public
   property MyProp[index: integer] read GetStuff write SetStuff
 end;

TChild = class...
 private
   procedure setStuff(index: integer; value: integer); override;
   function  getStuff(index: integer): integer; override;
 public
   property MyProp[index: integer] read GetStuff write SetStuff
 end;

procedure TChild.setStuff(value: integer);
begin
 inherited;      //  <-- execute parent 's code and
 DoMoreStuff;    //  <-- do some extra suff
end;

function TChild.getStuff;
begin
 result:= inherited;      
end;
A: 

I'm very rusty with Delphi. What kind of "it doesn't work" are you experiencing? Is it failing to compile?

I'd suspect that the inherited call wouldn't compile, because the parent doesn't really have a method to execute.

Carl Smotricz
+1  A: 

Solved. The child function implementation was wrong. Basically that code works. The solution was:

Result := inherited getStuff(Index);
Altar
To make this helpful for others you should probably say what was wrong. I guess you mean the `private` instead of a ´protected`?
Smasher
No, Smasher, the problem was in TChild.GetStuff, which tried to use bare `inherted` like a function. Delphi doesn't allow that. You need to specify the function name and parameters if you're going to use `inherited` as a function.
Rob Kennedy
Besides that, being private wouldn't have an effect in this case since both classes are apparently in the same unit. If they're in separate units, then the descendant class wouldn't compile because it wouldn't have access to the ancestor class's private virtual methods. (This differs from C++, where you're allowed to override private methods even though you're not allowed to call them.)
Rob Kennedy