This code is working correctly/logically - and the same as it does in Delphi...
When using "implements" you are directing that this property "Intf" provides the interface "IIntf1" for your host object "TClass2" - and in TClass2.Create you create object "O: TClass1" to implement interface "IIntf1". Since "I1" requests the "IIntf1" interface, it is provided via "implements" and is therefore implemented by object "O". When "I2:=C2 as IIntf2;" is executed, there is no "implements" property for this interface, so C2.GetInterface returns an "IIntf2" interface which it implements itself - and therefore "I2.Print" prints 3. When "I2:=I1 as IIntf2;" is executed, this calls O.GetInterface (as "O" is the implementing object) requesting "IIntf2", which O does support, so the returned "IIntf2" interface is still implemented by "O". When calling "I2.Print", "O" as the implementing object returns 30. When you comment out "implements", the only object implementing the interfaces you are working with is your main "C2" object - so 3 is the only value that can be returned. Regards, Andrew Hall. On 22 Nov 09, at 04:24 , Denis Golovan wrote: > I've got a non-obvious class hierarchy with interfaces involved. > The problem is that, I thought "property ... implements ...;" construction > fully substitute for implementing interface methods in class itself, but now > it's just not true. > I get different results in case implements is used and when it isn't. > > If I comment implements implementation, my results are: > TClassBase.Print 3 > TClassBase.Print 3 > TClassBase.Print 3 > > If I use implements, my results are: > TClassBase.Print 3 > TClassBase.Print 3 > TClassBase.Print 30 > > See my example below. > > I wonder if it is by design? > I have similar bug posted on bug tracker under > http://bugs.freepascal.org/view.php?id=14842 >
_______________________________________________ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal