[fpc-pascal] Interfaces via implements keyword

Andrew Hall andrew.hall at shaw.ca
Wed Nov 25 08:36:57 CET 2009


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
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20091124/11bcfda4/attachment.html>


More information about the fpc-pascal mailing list