[fpc-pascal] Turbo Pascal and Object Pascal ways of OOP

Daniël Mantione daniel.mantione at freepascal.org
Tue Feb 5 23:33:27 CET 2008



Op Tue, 5 Feb 2008, schreef Marc Weustink:

> Dani=EBl Mantione wrote:
>> =

>> =

>> Op Tue, 5 Feb 2008, schreef Luiz Americo Pereira Camara:
>> =

>>> can i safely use the below object  instead of the record and pass direc=
tly =

>>> to the c function?
>>> =

>>> TMyObj =3D object
>>> x: Integer;
>>> y: Integer;
>>> Method1;
>>> Method2;
>>> end;
>>> PMyObj =3D ^TMyObj;
>> =

>> Yes, objects (by specification) are defined to have the same binary layo=
ut =

>> as records.
>> =

>>> Moreover, whats the difference between objects and records with methods =

>>> (the new Delphi feature)?
>> =

>> Objects can have virtual methods, constructors, destructors. The new Del=
phi =

>> feature cannot.
>
> however with virtuals or constructors/destructors, the memory layout is n=
ot =

> 100% the same as a record.

A constructor doesn't add a vmtlink. A virtual method does, and a =

destructor too because they are virtual. It is never a problem in =

practise, because you can declare the virtual method in a descendent, =

i.e.:

type  Tbinary_compatible_obj=3Dobject
         a,b,c,d,e,f:byte;
         constructor init;
       end;

       Tbinary_compatible_obj_with_vmt=3Dobject(Tbinary_compatible_record)
         constructur init;
         procedure virtualmethod;virtual;
         desctructor done;virtual;
       end;

In the above example, you can perfectly pass a =

Tbinary_compatible_obj_with_vmt instance to any C library expecting a =

Tbinary_compatible_obj, because the vmtlink is located after the fields =

and therefore does not interfere with the binary structure.

Dani=EBl


More information about the fpc-pascal mailing list