Object oriented programming and interfaces
Jesse Phillips
Jesse.K.Phillips+D at gmail.com
Tue Dec 5 15:01:51 UTC 2017
On Monday, 4 December 2017 at 20:43:27 UTC, 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 think everyone here has missed the reason.
The problem is that a derived type can do more, the interface
only allows for two method calls while the class could
theoretically do more.
If the compiler allowed the class it would let you access
functions and members not available to the interfaces, and the
function would still be passed a Medoid.
You could make a cast within your function, but then you're still
really not handling the interface but that at least would be
clear in the code rather than the compiler hiding it by ignoring
the problem.
More information about the Digitalmars-d-learn
mailing list