Delphi Language Guide Delphi for Microsoft Win32 Delphi for the Microsoft. Net framework



Yüklə 0,84 Mb.
Pdf görüntüsü
səhifə171/294
tarix02.01.2022
ölçüsü0,84 Mb.
#41395
1   ...   167   168   169   170   171   172   173   174   ...   294
DelphiLanguageGuide

Destructors
A destructor is a special method that destroys the object where it is called and deallocates its memory. The
declaration of a destructor looks like a procedure declaration, but it begins with the word destructor. Example:
destructor SpecialDestructor(SaveData: Boolean); 
destructor Destroy; override;
Destructors on Win32 must use the default register calling convention. Although a class can have more than one
destructor, it is recommended that each class override the inherited 
Destroy
 method and declare no other
destructors.
To call a destructor, you must reference an instance object. For example,
MyObject.Destroy;
When a destructor is called, actions specified in the destructor implementation are performed first. Typically, these
consist of destroying any embedded objects and freeing resources that were allocated by the object. Then the
storage that was allocated for the object is disposed of.
Here is an example of a destructor implementation.
destructor TShape.Destroy; 
begin 
    FBrush.Free; 
    FPen.Free; 
    inherited Destroy
end;
The last action in a destructor's implementation is typically to call the inherited destructor to destroy the object's
inherited fields.
When an exception is raised during creation of an object, Destroy is automatically called to dispose of the unfinished
object. This means that 
Destroy
 must be prepared to dispose of partially constructed objects. Because a
constructor sets the fields of a new object to zero or empty values before performing other actions, class-type and
pointer-type fields in a partially constructed object are always nil. A destructor should therefore check for nil values
before operating on class-type or pointer-type fields. Calling the Free method (defined in TObject), rather than
Destroy
, offers a convenient way of checking for nil values before destroying an object.
Note:
For more information on constructors, destructors, and memory management issues on the .NET platform,
please see the topic Memory Management Issues on the .NET Platform.
Message Methods
Message methods implement responses to dynamically dispatched messages. The message method syntax is
supported on all platforms. VCL uses message methods to respond to Windows messages.
A message method is created by including the message directive in a method declaration, followed by an integer
constant between 1 and 49151 which specifies the message ID. For message methods in VCL controls, the integer
constant can be one of the Win32 message IDs defined, along with corresponding record types, in the 
Messages
unit. A message method must be a procedure that takes a single var parameter.
For example:
148


type 
    TTextBox = class(TCustomControl) 
      private 
       procedure WMChar(var Message: TWMChar); message WM_CHAR; 
       ... 
    end;
A message method does not have to include the override directive to override an inherited message method. In fact,
it doesn't have to specify the same method name or parameter type as the method it overrides. The message ID
alone determines which message the method responds to and whether it is an override.

Yüklə 0,84 Mb.

Dostları ilə paylaş:
1   ...   167   168   169   170   171   172   173   174   ...   294




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azkurs.org 2024
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin