[fpc-pascal] Class vs Object type

Tomas Hajny XHajT03 at hajny.biz
Wed Apr 6 14:07:07 CEST 2016


On Wed, April 6, 2016 13:26, Michael Van Canneyt wrote:
> On Wed, 6 Apr 2016, Graeme Geldenhuys wrote:

Hi,

>> I use Class and Object data types frequently in my code. I like the fact
>> that Object data types can be used without first instantiating an
>> instance and it gets freed automatically.
 .
 .
> 1. Classes are automatically zeroed out. That's an advantage.

True. Note that objects descended from TObject are automatically zeroed
out as well as soon as one calls the constructor Init.


> 2. When doing a:=b (a and B classes) you copy a pointer. This is cheap.
>     when doing the same with objects, you are potentially copying a lot of
> data,
>     with unknown side effects.

Well, one can work with pointers to objects as well if this is the
intention. You should always know whether you want to copy a reference to
certain data or whether you want to create copy of the stored data (the
latter obviously takes more time, but that doesn't matter much if this is
what you need to do anwyay). Otherwise you could equally say that copying
just a pointer may have "unknown side effects" as soon as you change the
"copy".

Tomas





More information about the fpc-pascal mailing list