4
type 
  TParent=class
    public
      member1:Integer;
  end;

  TChild=class(TParent)
  public
    member2:Integer;
  end;


  TGArray<T: TParent>=class
    public
      function test:T;
    end;

  implementation

  var g:TGArray<TChild>;

    function TGArray<T>.test:T;
    begin
      Result:=??.create; // <<<<  Problem !
    end;


  begin
    g := TGArray<TChild>.Create;
    g.test.member2 := 1234;
  end.

g.test はクラスのインスタンスを返す必要があります。私は複数のことを試しました:

1.  Result := Result.create; //Exception
2.  Result := TChildClass.Create; //Error
3.  type TGArray<T: class> = class; //and above 2. The same errors/exceptions.

これの目的は、クラスの汎用配列を作成することです。配列はジェネリック クラス内に格納され、インスタンスを返しますが、どのように?

これを達成できれば、コードを 3 回短縮できますが、それはできません。解決策を提案してください。

4

1 に答える 1

7

#2のエラーが何であるかはわかりませんが、コンストラクターの制約が必要であることを示しているに違いありません。 1つ追加すると、動作するはずです。

于 2010-05-14T18:02:41.003 に答える