[fpc-pascal] Class reference doubt

Luiz Americo Pereira Camara luizmed at oi.com.br
Fri Nov 25 02:04:04 CET 2011


On 24/11/2011 19:34, Jonas Maebe wrote:
> In that case, you did not hit the same problem as the original poster (your "I also hit this problem recently" is what triggered my response). His problem was that if you call a non-virtual constructor on a class reference variable, that the constructor is determined based on the static type of the class reference rather than on the dynamic type.

It does not matter much but i hit the same problem. Just replace class 
of TObj by class of TObject (TClass)

I stored a class (TMyClass) in a  class variable (AClass: TClass). I was 
expecting that calling AClass.Create would call TMyClass.Create. Just 
like him i found that is not the case.

To be clear: i'm not saying that is a bug or asking for changing the 
behavior

>> The task is accomplished by other means
>>
>> See http://code.google.com/p/emballo/source/browse/trunk/Src/Emballo.DI.Instantiator.pas#152
> That's indeed a completely different technique to dynamically instantiate classes (and if you don't really need that kind of flexibility, I would recommend virtual constructors instead since they're much simpler and faster).

I will do in a third way, that keeps flexibility (and is also fast) but 
requires more typing.

Luiz



More information about the fpc-pascal mailing list