[fpc-pascal] constructor as procvar
Michael Van Canneyt
michael at freepascal.org
Mon Mar 22 09:15:05 CET 2010
On Sat, 20 Mar 2010, David Emerson wrote:
> 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)
Try using Instance.ClassType. Something like:
Var
C : t_mammal_class;
MyClone : T_mammal;
begin
C:=t_mammal_class(Instance.ClassType);
MyClone:=C.Create;
end;
Michael.
>
> 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
>>
>
> _______________________________________________
> 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