Prof A Olowofoyeku (The African Chief) wrote:
By callng FillChar (Self, Sizeof (Self), 0) of something to that effect (for an example, see the initialisation of TObject in the FreeVision objects unit - for a GPC compilable version, see: http://www.gnu-pascal.de/contrib/chief/objects/objects-20031019.zip)
With GPC, doing this may well trash the VMT (Frank can tell you whether that is so or not).
Yes, it is:
program Foo;
type Foo = object constructor Init; end;
constructor Foo.Init; begin WriteLn (PtrInt (TypeOf (Self))); FillChar (Self, SizeOf (Self), 0); WriteLn (PtrInt (TypeOf (Self))) end;
var a: Foo;
begin a.Init end.
gives:
134775684 { or any other address } 0
Of course, you could save the VMT (SizeOf) and restore it afterwards (SetType), but ...
It will most certainly trash all fields that are strings.
And other schemata, and files, and objects (though objects as fields of another object may not be so common), and perhaps more in the future.
For non-object types you could use `Initialize' to re-init them, but in an object base type it only knows about the fields of the base type, so it won't help much.
Therefore, you are still left with initialising everything manually.
And doing this in each derived type as well.
It's generally much easier (and better style) to zero out those fields you really need to (which are usually not all fields).
Frank