[fpc-pascal] casting interfaces and objects

Marc Santhoff M.Santhoff at t-online.de
Sun May 18 23:24:09 CEST 2008

Am Sonntag, den 18.05.2008, 22:51 +0200 schrieb Graeme Geldenhuys:
> 2008/5/18 Marc Santhoff <M.Santhoff at t-online.de>:
> > When using a cast from an interface to an object the compiler warns
> > about the class types "TObject" and "IObserver" not being related.
> >
> > Since an implementor of in interface always will be a class type and any
> > class type at least inherits from TObject, I assume it is okay?
> >
> > Is there a better method of handling this case?
> Casting of Interfaces has always been known to me as a big NO-NO.

I learned casting in general is sort of evil, but not always avoidable.

>   I
> believe it's got something to do with how they are represented in the
> VMT or method table or something like that. I don't know the compiler
> internals.

I see, enough to remember "Do not cast interfaces!". ;)

> Why not simply use a InterfaceList or a generic Pointer
> list (as to not increment the interface reference counter). You can
> then add them to the list without having to cast to TObject.

That's what I will do.

Thank you,

More information about the fpc-pascal mailing list