22.12.2010 15:00, Sven Barth wrote:

Would you please test the following:
- identifier visibility if class helper and extended class reside in
different units (because I can't believe that a helper can access
private members while a derived class can't)

Class helper can access the same as derived class

- can overloaded methods be defined? (e.g. class contains "Test(a:
String);" and helper contains "Test(a: Integer);")

If overload is used then yes, if not then no. Look at test for more info.

- what happens if you define a method in the helper that has the same
name as a virtual method in the extended class (without "override" of
course ^^)

Look at test.

- is a class helper method used in a derived class if the derived class
does not contain a method with the same name?

Look at test.

- class helper and class in two different units, both containing a
method with the same name. which method get's called if the unit with
the class is used before the unit with the class helper in the main
program?

Will test later.

- can class helpers have fields and if so is their value saved somehow?

They can't.

- can class helpers extend interfaces?

How you see this?

- can a class helper reintroduce a virtual method from the extended class?

Yes.

Best regards,
Paul Ishenin
program tclasshelper2;

{$APPTYPE CONSOLE}

uses
  tuclasshelper1 in 'tuclasshelper1.pas';

type
  TFooHelper1 = class helper for TFoo
  public
    //F4: Integer; // [DCC Error] tclasshelper2.dpr(11): E2169 Field definition 
not allowed after methods or properties
    //var F4: Integer; // [DCC Error] tclasshelper2.dpr(12): E2029 'END' 
expected but 'VAR' found
    procedure TestAccess;
    procedure TestOverload(I: Integer);
    procedure TestOverload1(I: Integer); overload;
    function TestVirtual: Integer;
  end;

  TFoo2 = class(TFoo)
    //function TestVirtual: Integer; override; // [DCC Error] 
tclasshelper2.dpr(18): E2170 Cannot override a non-virtual method
    function TestVirtual1: Integer;
  end;

{ TFooHelper1 }

procedure TFooHelper1.TestAccess;
begin
  // F1 := 1; [DCC Error] tclasshelper2.dpr(18): E2003 Undeclared identifier: 
'F1'
  F2 := 1;
  F3 := 1;
end;

procedure TFooHelper1.TestOverload(I: Integer);
begin
  //
end;

procedure TFooHelper1.TestOverload1(I: Integer);
begin
  //
end;

function TFooHelper1.TestVirtual: Integer;
begin
  Result := 2;
end;

{ TFoo2 }

function TFoo2.TestVirtual1: Integer;
begin
  Result := TestVirtual;
end;

var
  F: TFoo;
  F1: TFoo1;
  F2: TFoo2;
begin
  F := TFoo.Create;
  //F.TestOverload('String'); // [DCC Error] tclasshelper2.dpr(33): E2010 
Incompatible types: 'Integer' and 'string'
  F.TestOverload1('String');
  F.TestOverload1(10);
  F.Free;

  // Test virtual reintroduced in helper

  F := TFoo1.Create;
  if F.TestVirtual <> 2 then
    halt(1);
  F.Free;

  F := TFoo2.Create;
  if F.TestVirtual <> 2 then
    halt(2);
  if TFoo2(F).TestVirtual1 <> 2 then
    halt(3);
  F.Free;

  F1 := TFoo1.Create;
  if F1.TestVirtual <> 3 then
    halt(4);
  F1.Free;

  F2 := TFoo2.Create;
  if F2.TestVirtual <> 2 then
    halt(5);
  if F2.TestVirtual1 <> 2 then
    halt(6);
  F2.Free;
  WriteLn('ok');
end.
unit tuclasshelper1;

interface

type
  TFoo = class
  private
    F1: Integer;
  strict protected
    F2: Integer;
  public
    F3: Integer;
    procedure TestOverload(S: String);
    procedure TestOverload1(S: String); overload;
    function TestVirtual: Integer; virtual;
  end;

  TFoo1 = class(TFoo)
    function TestVirtual: Integer; override;
  end;

implementation

{ TFoo }

procedure TFoo.TestOverload(S: String);
begin
  //
end;

procedure TFoo.TestOverload1(S: String);
begin
  //
end;

function TFoo.TestVirtual: Integer;
begin
  Result := 1;
end;

{ TFoo1 }

function TFoo1.TestVirtual: Integer;
begin
  Result := 3;
end;

end.
_______________________________________________
fpc-devel maillist  -  [email protected]
http://lists.freepascal.org/mailman/listinfo/fpc-devel

Reply via email to