[fpc-pascal] Class vs Object type

Michael Van Canneyt michael at freepascal.org
Wed Apr 6 13:26:13 CEST 2016



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.
>
> But is there a technical (maybe compiler) reason why I should possibly
> switch all my code to only using Class types for instance?
>
> It has to be said that for more complex "objects" I always use a Class
> type though. Also, Delphi compatibility (or their lack of Object type
> support) is of no concern to me.
>
>> From the FPC documentation I noted the following:
>
> "The difference between objects and classes is mainly that an object is
> allocated on the stack, as an ordinary record would be, and that classes
> are always allocated on the heap."
>
> Are there pros or cons to either?

1. Classes are automatically zeroed out. That's an advantage.

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.

Michael.



More information about the fpc-pascal mailing list