When should I use enhanced record types in Delphi

2019-01-31 14:25发布

Delphi 2006 introduced new capabilities for records, making them more 'object-oriented'.

In which situations is the record type more appropriate for a design than a class type? Which advantage does it have to use these record types?

4条回答
兄弟一词,经得起流年.
2楼-- · 2019-01-31 14:39

I think those features were also available in Delphi 8 and 2005.

Main guideline: if you're in doubt, use a class.

For the rest you have to understand the main difference: Class Objects are always used through a reference, and are created by calling a Constructor.

The memory management and allocation for Records is the same as for the basic types (ie integer, double). That means that they are passed to methods by value (unless var is used). Also you don't need to Free records, and that's the reason they support operator overloading. But no inheritance or virtual methods etc. The new Records can have a constructor but it's use is kind of optional.

The main areas and criteria for using records:

  • when dealing with structs from the Win32 API

  • when the types don't have identity (because assignment means copying)

  • when the instances aren't too large (copying big records becomes expensive)

  • when building value types, whose behaviour should mimic the numerical types. Examples are DateTime, Complex Numbers, Vectors etc. And then operator overloading is a nice feature, but don't make that the deciding factor.

And efficiency-wise, don't overdo this:

  • for smaller types that you put in arrays often.

And finally, the rules for using a class or a records haven't really changed form the earlier versions of Delphi.

查看更多
\"骚年 ilove
3楼-- · 2019-01-31 14:48

In addition to the other answers (operator overloading, lightweight value types), it's a good idea to make your enumerators records instead of classes. Since they're allocated on the stack, there's no need to construct and destruct them, which also removes the need for the hidden try..finally block that the compiler places around class-type enumerators.

See http://hallvards.blogspot.com/2007/10/more-fun-with-enumerators.html for more information.

查看更多
【Aperson】
4楼-- · 2019-01-31 14:51

You have records, objects and classes.

Records are available since turbo pascal 1. They are lightweight, capable of having properties and methods, but they do not support inheritance, There are some issues with functions that return records. If these records have methods this sometimes gives internal errors:

type
  TRec = record 
    function Method1: Integer;
  end;

function Func: TRec;


procedure Test;
var
  x : TRec;

begin
  Func.Method1; // Sometimes crashes the compiler
  // Circumvention:
  x := Func;
  x.Method1; // Works
end;

Objects are introduced with turbo pascal 5 if I'm correct. They then provided a way for OO with pascal. They are more or less deprecated with the introduction of Delphi, but you can still use them. Objects can implement interfaces.

Classes are introduced with Delphi 1 and the most versatile. They implement interfaces and support inheritance. But each class variable is a hidden pointer. This means that classes need to be created on the heap. Luckily this process is mostly hidden.

Below is a table with the differences between the three. I added the interface for completion.

                  |Class|Object|Record|Interface|
------------------|-----------------------------|
Are pointers?     |  y  |  n   |  n   |    y    |
Inheritance       |  y  |  y   |  n   |    y    |
Helpers           |  y  |  n   |  y   |    n    |
Impl. Interface   |  y  |  y   |  n   |    -    |
Visibility        |  y  |  y   |  n   |    n    |
Method            |  y  |  y   |  y   |    y    |
Fields            |  y  |  y   |  y   |    n    | 
Properties        |  y  |  y   |  y   |    y    |
Consts            |  y  |  y   |  y   |    n    |
Types             |  y  |  y   |  y   |    n    |
Variants          |  n  |  n   |  y   |    n    |
Virtual           |  y  |  n   |  y   |    -    |
------------------|-----------------------------|
查看更多
\"骚年 ilove
5楼-- · 2019-01-31 14:57

You can use operator overloading (like implicit conversions). This you can do on Delphi 2007+ or 2006.NET on objects too, but only on these records on 2006 win32.

查看更多
登录 后发表回答