Delphi 2010 : Confusing generic type TList scenario? 按值传递还是引用传递?

标签 delphi generics record tlist

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

最佳答案

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

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;

关于Delphi 2010 : Confusing generic type TList scenario? 按值传递还是引用传递?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6057298/

相关文章:

windows - Delphi:如何使用驱动器盘符以编程方式弹出 CD

具有关联类型的 Swift 子协议(protocol)

.net-core - 为什么散列 F# 记录在每次运行时返回不同的值

java - 在Java中,匿名类可以声明自己的类型参数吗?

Oracle PL/SQL 变量记录字段名称

python - Pandas :如何计算考虑到以前记录的变化数量

delphi - 如何让 TAnimate 的通用 AVI 在 Vista 和 Win7 上运行?

delphi - IntToStr未声明的标识符错误

multithreading - 锁定多个读者单个作家

java - 来自抽象、通用外观的 JPA Controller 类的具体外观