[fpc-devel] Language extension: absolute for classes
Michalis Kamburelis
michalis at camelot.homedns.org
Sun Oct 1 14:18:44 CEST 2006
Micah Milissent wrote:
> Hi,
>
> I want to bring up the following scenario: (need fixed font)
>
> B --> G
> | |
> A --> F
>
> All are classes, and usually A 'owns' F. So A has a field 'Field' of
> type F. Now, whenever A creates F, B overrides this (in virtual method
> or class type) to create a G.
>
> The problem now is that every time B wants to access G, it has to
> typecast Field to G.
>
You don't need new language construction for this ?
All you want is just to cover in class B identifier "Field" of class A.
So you should make "Field" a dummy function in class A (that just
returns a field value), and then you can redefine function name in
descendant classes. See the example below. Within the scope of class B
the "Field" function will return class G.
---------
{$mode objfpc}
type
TF = class
end;
TG = class(TF)
end;
TA = class
private
FField: TF;
protected
function Field: TF;
end;
function TA.Field: TF;
begin
Result := FField;
end;
type
TB = class(TA)
protected
function Field: TG;
end;
function TB.Field: TG;
begin
Result := (inherited Field) as TG;
end;
begin
end.
---------
Michalis
More information about the fpc-devel
mailing list