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
_______________________________________________
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-devel

Reply via email to