[fpc-pascal] Class reference doubt

Luiz Americo Pereira Camara luizmed at oi.com.br
Fri Nov 25 16:42:17 CET 2011


On 25/11/2011 07:50, Dimitri Smits wrote:
> so you have:
> ====
> type
>    TMyObject=class(TObject)
>     ...
>     constructor Create;
>    end;
>

Yes

>    TMyClass = class of TMyObject; //<-- important I guess
>

Not important or necessary. I was using TClass from RTL = class of TObject

> ...
>
> procedure processSomething(AClass: TClass);
> var
>    someInstance: TObject;
> begin
>    someInstance := AClass.Create(); //<-- always the one from TClass/TObject?
> end;
>

Yes

> ...
> begin
>    processSomething(TMyObject);
> end;
>
> ====
>
> class methods (and the constructors and destructor) are in Delphi part of the TClass memorystructure. I believe even the TObject default Create. For your trick to work, you need one of 2 things:
> - declare the class-reference for a type explicitly (class of T...)
> - make a virtual constructor in a subtype of TObject and declare a class-reference. Then you derive from that subtype.

I know all of this. I even already use this approach in one of my 
libraries where i have a base class and a reference to class of this type.

What i needed this time was more generic: the ability to instantiate any 
TObject without the need to change interface of the classes

> I don't think (did not test) it finds a TMyClass in Delphi as well if you do not declare the type, so in effect it always takes TClass. The other scenario's I've used before in classfactory pattern before.
>
> It has nothing and everything to do with RTTI. No, the new D2010+ RTTI does not give access to the default constructor (I think),

Yes, it does see example at 
http://code.google.com/p/emballo/source/browse/trunk/Src/Emballo.DI.Instantiator.pas#152

Anyway, i already found a solution, so there's no fuzz in my part

Luiz



More information about the fpc-pascal mailing list