Object oriented programming and interfaces

Jacob Carlborg doob at me.com
Mon Dec 4 21:08:08 UTC 2017


On 2017-12-04 21:43, Dirk wrote:
> Hi!
> 
> I defined an interface:
> 
> interface Medoid {
>      float distance( Medoid other );
>      uint id() const @property;
> }
> 
> and a class implementing that interface:
> 
> class Item : Medoid {
>      float distance( Item i ) {...}
>      uint id() const @property {...}
> }
> 
> The compiler says:
> Error: class Item interface function 'float distance(Medoid other)' is 
> not implemented
> 
> Is there a way to implement the Item.distance() member function taking 
> any object whose class is Item?

I don't think so. In the "Item" class you have declared "distance" to 
take an instance of "Item", which is more specialized than "Medoid". If 
an instance of "Item" is used through the "Medoid" interface, that 
interface allows to pass any object to the "distance" method that 
implements the "Medoid" interface. That can be a different class than 
"Medoid".

You could add an overload that takes an instance of "Item", if that helps.

-- 
/Jacob Carlborg


More information about the Digitalmars-d-learn mailing list