Delphi2007新功能 -- 有限的栈对象

 今天使用Delphi2007,一个误输入,无意中发现Delphi2007的record类型居然能够和TObject一样定义方法和属性,而且不需要调用类似TObject.Create方法就能生成一个record对象。这是否意味着Delphi2007也能和C++一样,不仅能使用堆对象(new),也能使用栈对象(静态对象)呢?

        通过实验,答案是肯定的!这使我感到很兴奋,因为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;

        可以看出,确实可以和C++一样定义一个局部record对象(不需要动态建立),而且可以使用该对象的方法和属性,请看TForm1.Button1Click方法的汇编码(CPU调试窗口粘贴下来的):

Unit1.pas. 72 : begin
0045ACDC 83C4F8           add esp,
- $ 08
Unit1.pas.
73 : rec.TMyRecord( 100 200 );
0045ACDF 8BC4             mov eax,esp
0045ACE1 B9C8000000       mov ecx,$000000c8
0045ACE6 BA64000000       mov edx,$
00000064
0045ACEB E8E4FFFFFF       call TMyRecord.TMyRecord
Unit1.pas.
74 : rec.Display;
0045ACF0 8BC4             mov eax,esp
0045ACF2 E855FFFFFF       call TMyRecord.Display
Unit1.pas.
75 : end;
0045ACF7 
59                pop ecx
0045ACF8 5A               pop edx
0045ACF9 C3               ret

        从汇编码中看,定义的TMyRecord变量rec确实是标准C++栈对象一样的,在调用其方法的时候也隐含传递了Self指针(mov  eax,esp),也就是说,rec在这里已经区别于普通的record变量,而是一个实实在在的“对象”变量!

        但是遗憾的是,它不能定义destructor方法,虽然可以定义constructor方法(不能定义为Tobject缺省的Create方法,带参数可以),但却不能在定义的时候自动调用构造方法(我原本以为和C++一样,定义一个和TMyRecord同名的constructor方法,可以自动调用的),如此一来,静态栈对象的主要优点就丧失了,如果手工调用构造方法,和定义一个普通record变量,然后调用静态过程没什么两样了。其实,Delphi已经把record改道这一步了,稍稍进一步就可以完完全全的使用栈对象,从技术上讲一点难度都没有,编译器只需要在栈中分配对象变量内存和销毁对象变量内存的同时,隐含调用一下构造方法和析构方法就可以了的。

        虽然很遗憾,但是还是为Delphi的这点进步而高兴,至少我们可以利用这一功能封装record,而且使用这有限的栈对象可以不用当心对象本身的销毁问题(record对象中动态分配的内存还是得手工释放或者调用它的某个方法释放)。

        后记:以前在DOS下,以C/C++为主,Pascal只用过Turbo Pascal4.0,对以后Pascal的版本并不十分了解,今天看了网友housisong的留言,才知道我所谓的新功能--有限栈对象,Delphi中早就有了,如Delphi7.0就可以使用Object关键字定义栈对象,而且比2007的record类型还完善一些,record不能继承,而object对象能够继承,看来我真是孤陋寡闻了!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
IDE Fix Pack 2007 - Version 3.0 (2010-03-15) Installation: ============= 1. Start IDEFixPackReg.exe under your user account 2. Select the IDE registry keys for which you want to install the bugfix 3. Press the "Install" button Uninstallation: =============== 1. Start IDEFixPackReg.exe under your user account 2. Press the "Uninstall" button. This will uninstall the bugfix for all (not only the selected) IDE registry keys. Changelog: ========== 2010-03-15: - Added fix for: QC #80822: ObjectInspecor: Properties are duplicated after scrolling - Added fix for: QC #80776: ObjectInspector shows "EditControl" instead of the real content - Added fix for: QC #79776: Clicking on object Inspector rejects focus 2009-12-22: - Added fix for: QC #75738: Debugging extremly slow - Added fix for: QC #68493: Switching away and back to Delphi orphans focus on Code Editor 2009-12-05: - Added fix for: Vista compatible main icon resource doesn't work 2009-09-03: - Added fix for: 64 bit Debugger assertion - Added fix for: Undo destroyed editor buffer - Added fix for: Vista 64 IDE startup delay - Added: QC #74646: Buffer overflow in TCustomClientDataSet.DataConvert with ftWideString - Fixed: TTabSheet looked strange if used with SilverThemes 2009-03-03: - Fixed: The AppDeActivateZOrder patch now fixes the cause instead of the symptoms - Added fix for: Error Insight fails to find TObject class - Added fix for: Possible deadlock when Error Insight calls ProcessMessages 2009-02-18: - Added for for IDE may select the wrong file when performing a ctrl+click on a filename in the editor - Added faster AnsiCompareFileName replacement function which speeds up the "Install Packages..." dialog 2009-02-05: - Added: fix for "Cannot resolve unit name" Error Insight bug. 2009-01-25: - Fixed: C++Builder compilation slow down caused by the ReadWrite mode fix - Fixed: DBGrid ScrollBar gab wasn't painted correctly in BiDiMode <> bdLeftToRight - Fixed: TTabSheet could throw an access violation if no PageControl was assigned to it 2008-11-24: - Added fix for Show Component Caption IDE bug - Added fix for IDE Compiler opens all files in ReadWrite mode and blocks command line compiler 2008-07-19: Added: Control resize fix replaces the old editor resize bugfix Added: Background Parser now stops if the main thread wants to do something with the compiler. 2008-05-12: Added: Fix for Find dialog has problems with Umlaut chars Removed: CodeCompletion is fixed in RAD Studio 2007 April Update 2008-04-02: Added: Added fix for TCustomActionList.Notification memory overwrite Added: Fixed some TDBGrid and TPageControl form designer flicker Improved: Application window tastbar button removal (should now work for all Windows versions) 2008-02-18: Added: CodeCompletion patch to show inherited items 2008-02-10: Added Application window tastbar button removal for Vista Added TDBText Color bugfix Renamed to IDE Fix Pack 2008-02-04: Initial release as IDEWin64Fix

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值