Today, using Delphi2007, a false input, inadvertently discovered that the Delphi2007 record type can define methods and properties like TObject, and you can generate a record object without calling a similar tobject.create method. Does this mean that Delphi2007 can also be used in the same way as C + + to not only use heap objects (new), but also stack objects (static objects)?
Through the experiment, the answer is YES! This makes me very excited, because Delphi, from the date of birth, limits the heap objects that can only inherit from TObject, and must create and destroy an object through create and free, rather than using a stack object. The advantage of a stack object is that it can be defined as easily as other types, it can automatically call constructors and destructors, and within scope (such as a local object within a function) do not have to be careful about object creation and destruction. If Delphi has this function, it is not intended to be the Gospel of Delphi programmers.
However, further testing is rather disappointing, see the following code:
typeTmyrecord=Record PrivateX: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); ConstructorTmyrecord;Overload; ConstructorTmyrecord (A, B:integer);Overload;//Constructor Tmyrecord (A, b:integer); procedureDisplay; PropertyXi:integerReadx; End;{Tmyrecord}ConstructorTmyrecord.Create(A:integer);beginx:=A; Y:=0;End;procedureTmyrecord.display;beginShowMessage (Format ('x =%d, y =%d', [x, y]);End;ConstructorTmyrecord.tmyrecord;beginx:=0; Y:=0;End;ConstructorTmyrecord.tmyrecord (A, b:integer);beginx:=A; Y:=b;End;procedureTform1.button1click (sender:tobject);varRec:tmyrecord;beginRec. Tmyrecord ( -, $); Rec. Display;End; As you can see, it's true and C++define a local Record object (which does not need to be built dynamically), and you can use the method and properties of the object, see the assembly code for the Tform1.button1click method (the CPU debug window is pasted down): Unit1.pas. the:begin0045ACDC 83c4f8 add ESP,-$ ,Unit1.pas. the: Rec. Tmyrecord ( -, $) 0045ACDF 8BC4 mov eax,esp0045ace1 b9c8000000 mov ecx,$000000c80045ace6 BA64000000 mov edx,$< /c9>000000640045ACEB e8e4ffffff call TMyRecord.TMyRecordUnit1.pas. About: Rec. DISPLAY;0045ACF0 8BC4 mov eax,esp0045acf2 e855ffffff call TMyRecord.DisplayUnit1.pas. the:End; 0045ACF7 -pop ecx0045acf8 5A pop edx0045acf9 C3 ret
From the sink code, the definition of the Tmyrecord variable rec is really the same as the standard C + + stack object, when calling its method also implicitly passed the self pointer (mov eax,esp), that is, REC here has been distinguished from the ordinary record variable, but a real "Object" Variable!
Unfortunately, it cannot define the destructor method, although it is possible to define the constructor method (which cannot be defined as the default create method for TObject, with parameters), but it is not possible to automatically invoke the constructor at the time of definition (I originally thought that, like C + +, Define a constructor method with the same name as Tmyrecord, which can be automatically called), so that the main advantage of the static stack object is lost, if you call the constructor manually, and define a normal record variable, then call the static process is no different. In fact, Delphi has the record diversion this step, a little further can completely use the Stack object, from the technical point of no difficulty, the compiler only need to allocate the object variable memory in the stack and destroy the object variable memory, implicitly call the construction method and the destructor can be.
It's a pity, but it's still nice to see the progress of Delphi, at least we can use this feature to encapsulate the record, and using this limited stack object is not to be careful about the destruction of the object itself (the dynamically allocated memory in the Record object is either manually freed or a method that calls it to release).
PostScript: In the past, in DOS, C + +, Pascal only used Turbo Pascal4.0, after the version of Pascal is not very understanding, today saw the Netizen housisong message, To know what I call a new feature-Finite stack object, Delphi has long ago, such as Delphi7.0 can use the Object keyword to define the stack object, and more than 2007 of the record type is also perfect some, record can not inherit, and object object can inherit, it seems I really ignorant!!!
Delphi2007 new Features--limited stack objects