Delphi2007新功能-有限的棧對象

來源:互聯網
上載者:User

通過實驗,答案是肯定的!這使我感到很興奮,因為Delphi從誕生之日起,就限定了只能繼承於TObject的堆對象,必須通過Create和Free來建立和銷毀一個對象,而不能使用棧對象。而棧對象的好處就在於它能和其它類型一樣很方便的定義,它能自動調用建構函式和解構函式,在範圍範圍內(如函數內的局部對象)不必當心對象的建立和銷毀問題。要是Delphi具有了該功能,無意是Delphi編程者們的福音。

不過,進一步的測試卻比較失望,請看下面的代碼:

type
TMyRecord = record
private
x: Integer;
y: Integer;
public
// error: Parameterless constructors not allowed record types
// constructor Create;
// error: A record cannot introduce a destructor
// destructor TMyRecird;
constructor Create(a: Integer);
constructor TMyRecord; overload;
constructor TMyRecord(a, b: Integer); overload;
// constructor TMyRecord(a, b: Integer);
procedure Display;
property xi: Integer read x;
end;
{ TMyRecord }
constructor TMyRecord.Create(a: Integer);
begin
x := a;
y := 0;
end;
procedure TMyRecord.Display;
begin
ShowMessage(Format('x = %d, y = %d', [x, y]));
end;
constructor TMyRecord.TMyRecord;
begin
x := 0;
y := 0;
end;
constructor TMyRecord.TMyRecord(a, b: Integer);
begin
x := a;
y := b;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
rec: TMyRecord;
begin
rec.TMyRecord(100, 200);
rec.Display;
end;

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.