Understanding the Delphi class (1)-starting from the structure

Source: Internet
Author: User
Unit unit1; interfaceuses windows, messages, extensions, variants, classes, graphics, controls, forms, dialogs, stdctrls; Type tform1 = Class (tform) button1: tbutton; Procedure button1click (Sender: tobject); end; var form1: tform1; implementation {$ R *. DFM} type tmyrecord = record {definition structure} D: tdate; end; tmyclass = Class {definition class; although this class has no practical significance, it is a class} D: tdate; end; Procedure tform1.button1click (Sender: tobjec T); var myrec: tmyrecord; {declare a structure variable} mycls: tmyclass; {declare a class variable, that is, object} begin {use structure} myrec. d: = now; showmessage (datetostr (myrec. d); {} showmessage (inttostr (sizeof (myrec); {8; the size of myrec structure is 8} {use class} mycls: = tmyclass. create; {need to be created manually first, because the class needs to be instantiated as an object before it can be used} mycls. d: = now; {2008-1-11} showmessage (datetostr (mycls. d); showmessage (inttostr (sizeof (mycls); {4; how can it be 4? Because the mycls object is just a pointer !} Mycls. free; {release after use} {conclusion: the structure (or record) and class are both custom types. The structure is directly used and the memory is automatically managed. The class is just a model, you need to create (create) an object based on the model (that is, the class) to use. You need to manually release the memory after use. (The memory usage rules should be: the structure of automatic release and manual release is stored in a different way than that of the object (class instance) in the memory. The object is just a pointer. Compared with the structure, class has this more powerful function !} End; end.
 
  
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.