[fpc-pascal] constructor as procvar

David Emerson dle3ab at angelbase.com
Sat Mar 20 18:26:25 CET 2010


How can I obtain a class type variable from an instance? I want to 
create a second instance of the same descendant class (via the 
constructor, which will take some parameters and make the new instance 
unique)

Tobject.classtype returns TClass (class of TObject) which I first 
assumed would work great. However I cannot seem to do a type conversion 
from TClass to t_mammal_class (class of t_mammal)! So I am stuck here.

I tried just using TClass and skipping t_mammal_class, but then I can't 
use my overriden constructor that takes special parameters.

Thanks in advance,
David.


On Fri 19 Mar 2010, David Emerson wrote:
> Florian Klaempfl wrote:
> > Constructor procvars are indeed not supported but the way to achieve
> > what you want is to use class type variables
> > 
> >   t_mammal_class = class of t_mammal;
> > 
> >   function find_or_create_animal (color : byte;
> >       pass_mammal_type : t_mammal_class) : t_mammal;
> > 
> >   brown_pig := t_pig (pig_pen.find_or_create_animal (brown, t_pig));
> 
> ah, that is exactly what I need. Works perfectly. Thanks much.
> 
> ~D.
> 
> _______________________________________________
> fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
> http://lists.freepascal.org/mailman/listinfo/fpc-pascal
> 




More information about the fpc-pascal mailing list