Delphi 2010: сбивает с толку сценарий универсального типа TList? Передать по значению или ссылке? - PullRequest
2 голосов
/ 19 мая 2011

Я столкнулся с проблемой несколько дней назад, работая с Generic TList в середине проекта. Я проверил это в простом тестовом проекте и получил ту же проблему. Вот точный код:

type
  TMyPoint = record
    x: Integer;
    y: Integer;
  end;

  TShape = record
    Corners: TList<TMyPoint>;
  end;

  TForm1 = class(TForm)
    Button1: TButton;
    Label1: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    Shape_List: TList<TShape>;
  public
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  Shape: TShape;
  FirstPoint: TMyPoint;
  SecondPoint: TMyPoint;
  Temp: TMyPoint;
begin
  // Create the corners list
  Shape.Corners := TList<TMyPoint>.Create;
  // Add the first point to corners
  FirstPoint.x := 10;
  FirstPoint.y := 20;
  Shape.Corners.Add(FirstPoint);
  // Add the shape to the Shape_List
  Shape_List.Add(Shape);

  // Clear the shape corners
  Shape.Corners.Clear;

  // Add another point to corners
  SecondPoint.x := 100;
  SecondPoint.y := 200;
  Shape.Corners.Add(SecondPoint);

  // Show the x of the first point of the first shape
  Label1.Caption := IntToStr(Shape_List[0].Corners[0].x);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Shape_List := TList<TShape>.Create;
end;

Label1.Caption будет 100 , а не 10 ! Почему это так? Я думал, TList.Add(const value) это передача по значению, а не передача по ссылке!

Ответы [ 3 ]

4 голосов
/ 19 мая 2011

Добавлены некоторые комментарии к вашей процедуре, чтобы точно указать, где находится «ошибка».

procedure TForm1.Button1Click(Sender: TObject);
var
  Shape: TShape;
  FirstPoint: TMyPoint;
  SecondPoint: TMyPoint;
  Temp: TMyPoint;
begin
  // Create the corners list
  Shape.Corners := TList<TMyPoint>.Create; // We create a new TList<TMyPOint> OBJECT. Shape.Corners is a reference
  // Add the first point to corners
  FirstPoint.x := 10;
  FirstPoint.y := 20;
  Shape.Corners.Add(FirstPoint); // Add FirstPoint to the list we created at step 1.
  // Add the shape to the Shape_List
  Shape_List.Add(Shape); // Add a copy of the Shape record to the Shape_List

  // Clear the shape corners
  Shape.Corners.Clear; // Clear the Shape.Corners reference. This effectively clears the list of corners
                       // in the Shape you just added to Shape_List because it contains the same
                       // reference.

  // Add another point to corners
  SecondPoint.x := 100;
  SecondPoint.y := 200;
  Shape.Corners.Add(SecondPoint); // Add a new point to the Corners list. Remamber, Corners is actually
                                  // a reference. The first Shape you added to Shape_List contains a copy
                                  // of this exact same reference, so you're effectively adding a first point
                                  // to both Shape.Corners and Shape_List[0].Corners.

  // Show the x of the first point of the first shape
  Label1.Caption := IntToStr(Shape_List[0].Corners[0].x); // Yup, you just added that point, so you get 100
end;
4 голосов
/ 19 мая 2011

После добавления FirstPoint вы Clear Corners (после этого список пуст). Затем вы добавляете SecondPoint, который становится первым элементом (индекс 0).

Редактировать: Для иллюстрации:

var
  Shape1, Shape2: TShape;
begin
  Shape1.Corners := TList<TMyPoint>.Create;
  Shape2 := Shape1;
  OutputDebugString(PChar(Format('Shape1.Corners: $%.8x', [Integer(Shape1.Corners)])));
  OutputDebugString(PChar(Format('Shape2.Corners: $%.8x', [Integer(Shape2.Corners)])));
end;

Shape1.Corners и Shape2.Corners указывают на один и тот же список.

1 голос
/ 19 мая 2011

Нет, он передается по ссылке, поскольку передается как постоянный параметр (см .: http://docwiki.embarcadero.com/VCL/en/Generics.Collections.TList.Add).

...