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 l
Hi
The essential part is to have virtual constructor like in Florian Klaempfl's
example code:
t_mammal = class
public
constructor create (color : byte); virtual;
end;
and then...
t_pig = class(t_mammal)
public
constructor create (color : byte); override;
end;
t
Hi David,
If I'm reading your need correctly...
function CopyMyMammal(value: T_Mammal; extraParameters): T_Mammal;
begin
result := T_Mammal_Class(value.ClassType).Create(withExtraParameters);
end;
Only confusion is you say you cannot make the type conversion? But the above
code is correct and
> 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.
well, I got this working, not sure why I had so much trouble with a
simple typ
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)
Tobject.classtype returns TClass (class of TObject) which I fir
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
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;
>
>
> As you can (probably) see, I would like t_barnyard to be able to create
> any descendant of t_mammal. In the above example, I want it to create a
> brown pig if it doesn't find one, so I tried to pass the t_pig
> constructor, along with the brown parameter that said constructor will
> require
I'd like to pass a constructor as a procvar. Don't know if this is
possible. Here is some silly illustrative code that doesn't work:
{$mode objfpc}
uses classes, sysutils;
type
t_mammal = class
public
constructor create (color : byte); virtual;
end;
t_pig = class (t_mammal)