Delphi 2010:令人困惑的泛型类型 TList 场景?按值传递还是按引用传递?

发布于 2024-11-07 22:02:16 字数 1422 浏览 5 评论 0原文

几天前,我在项目中使用 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) 是按值传递而不是按引用传递!

I encountered a problem some days ago while working with Generic TList in the middle of a project. I tested it in a simple test project and got the same problem. Here is the exact code:

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;

The Label1.Caption will be 100 not 10 ! Why is it like this? I thought TList.Add(const value) is pass-by-value not pass-by-reference!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

So尛奶瓶 2024-11-14 22:02:16

在您的程序中添加了一些注释,以准确指出“错误”所在。

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;

Added some comments to your procedure to point out exactly where the "bug" is.

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;
勿忘初心 2024-11-14 22:02:16

添加 FirstPoint 后,您清除 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.CornersShape2.Corners 指向同一列表。

After adding the FirstPoint, you Clear the Corners (after that, the list is empty). Then you add SecondPoint which becomes the first element (index 0).

Edit: To illustrate:

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 and Shape2.Corners point to the same list.

萌梦深 2024-11-14 22:02:16

不,它是通过引用传递的,因为它作为常量参数传递(请参阅:http://docwiki.embarcadero.com/VCL/en/Generics.Collections.TList.Add)。

No, it is passed by reference since it is passed as a constant parameter (see: http://docwiki.embarcadero.com/VCL/en/Generics.Collections.TList.Add).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文