[fpc-pascal]init a class from another class

Jonas Maebe jonas at zeus.rug.ac.be
Mon Oct 8 20:54:55 CEST 2001


On maandag, oktober 8, 2001, at 08:35 , Ivan Montes wrote:

> tCoDec = CLASS
>   constructor init; VIRTUAL;
>   destructor done; VIRTUAL;
> end;

When using classes, you should use "create" as constructor name and you 
*must* use "destroy" as destructor name (if you don't do the latter, the 
destructor won't be called automatically when the class instance is 
freed). Also, your destructor should be declared as "override", because 
the destructor over TObject (of which all classes are derived) is 
already virtual. Have a look at the manuals to see the differences 
between "virtual" and "override" (in short: the first time a virtual 
method appears in a class hierarchy, you should declare it as "virtual" 
and afterwards, when you want to override it in a child class, use 
"override" instead).

Add the following to your program for the solution:

tTypeOfCoDec = class of tCoDec;


tMain.registerCoDec(var cd: tCoDec; mytype: tTypeOfCoDec);
begin
   cd := mytype.create;
  AddToCoDecsList(cd);
end;


Jonas





More information about the fpc-pascal mailing list