[fpc-pascal] Class reference doubt

Jonas Maebe jonas.maebe at elis.ugent.be
Thu Nov 24 23:34:08 CET 2011


On 24 Nov 2011, at 23:00, Luiz Americo Pereira Camara wrote:

> On 24/11/2011 08:02, Jonas Maebe wrote:
>> 
>> On 24 Nov 2011, at 02:58, Luiz Americo Pereira Camara wrote:
>> 
>>> On 23/11/2011 18:48, Jesus Reyes wrote:
>>>> where I would expect:
>>>> cls class is TFoo
>>>> TObj.create
>>>> TFoo.create
>>> 
>>> I also hit this problem recently
>>> 
>>> Found that this is one limitation of fpc. Under newer delphi it's possible to get the expected behavior without forcing programmer to create a virtual constructor by using the new RTTI
>> 
>> I find it very hard to believe that the behaviour of existing code like that would suddenly change (and hence potentially break programs) simply because extra RTTI is added.
> 
> It does not change.

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.

> 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).


Jonas


More information about the fpc-pascal mailing list