- Mastering Delphi Programming:A Complete Reference Guide
- Primo? Gabrijel?i?
- 290字
- 2021-06-24 12:33:36
Classes
While classes look superficially similar to records, they are in fact implemented in a completely different manner.
When you create an object of a class by calling the constructor for that class, the code allocates memory from the memory manager and fills it with zeroes. That, in effect, initializes all fields, managed and unmanaged, to default values. A pointer to this memory is stored in the variable which receives the result of a constructor.
When an object is destroyed, its memory is returned to the system (again, through the memory manager).
The rest of this section applies only to standard compilers (Window 32- and 64-bit and OS/X). If you are using an ARC-enabled compiler (Android, iOS, and Linux) then classes behave the same as interfaces. See the next section for more details.
Copying one variable of a class type to another just copies the pointer. In effect, you then have two variables pointing to the same object. There is no reference counting associating with objects and if you now destroy one variable (by calling the Free method), the other will still point to the same part of memory. Actually, both variables will point to that unused memory and if it gets allocated later (and it will), both variables will point to memory belonging to another object:
var
o1, o2: TObject;
begin
o1 := TObject.Create;
o2 := o1;
// o1 and o2 now point to the same object
o1.Free;
// o1 and o2 now point to unowned memory
o1 := nil;
// o2 still points to unowned memory
end;
If you have a long-living variable or field containing an object of some class and you destroy the object, make sure that you also set the value/field to nil.
- 24小時學會電腦組裝與維護
- 深入理解Spring Cloud與實戰
- FPGA從入門到精通(實戰篇)
- Deep Learning with PyTorch
- 筆記本電腦維修不是事兒(第2版)
- Creating Flat Design Websites
- Blender Quick Start Guide
- Building Machine Learning Systems with Python
- 微服務實戰
- 嵌入式系統設計大學教程(第2版)
- Arduino案例實戰(卷Ⅳ)
- Learn Qt 5
- Unreal Engine 4 AI Programming Essentials
- 數據恢復與硬盤修理
- Mastering JavaServer Faces 2.2