问题来源: http://www.cnblogs.com/del/archive/2008/12/29/1230404.html#1416838

譬如有两个 TStringList 对象: List1 和 List2;

List2.Assign(List1) 就是把 List1 的内容复制给 List2;

List2 := List1 相当于给 List1 再取一个名字.

举例:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  List1,List2: TStringList;
begin
  List1 := TStringList.Create;
  List2 := TStringList.Create;

  List1.CommaText := 'AAA,BBB,CCC';

  List2.Assign(List1); {此时 List1、List2 分别指向了不同的对象}
  ShowMessageFmt('%s, %p', [List1[0], Pointer(List1)]); { AAA, 00ABAA40 }
  ShowMessageFmt('%s, %p', [List2[0], Pointer(List2)]); { AAA, 00ABAA90 }

  List1.Free;
  ShowMessage(List2[1]); {List1 释放后, List2 还会存在.}
  List2.Free;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  List1,List2: TStringList;
begin
  List1 := TStringList.Create;
  List1.CommaText := 'AAA,BBB,CCC';

  List2 := List1;          {此时 List1、List2 指向了同一个对象}
  ShowMessageFmt('%s, %p', [List1[0], Pointer(List1)]); { AAA, 00ABAA40 }
  ShowMessageFmt('%s, %p', [List2[0], Pointer(List2)]); { AAA, 00ABAA40 }

  List1.Free;              {此时 List1、List2 指向的对象释放了}
  //ShowMessage(List2[1]); {所以这句会出错}
end;

end.

posted on 2008-12-29 21:09  万一  阅读(3139)  评论(0编辑  收藏  举报