Differing implementations for a function in two interfaces

BCS BCS_member at pathlink.com
Sat Apr 15 13:10:54 PDT 2006


In article <e1riem$3jd$1 at digitaldaemon.com>, Hasan Aljudy says...
>
>I didn't read/undertstand your entire post, but I think what you're 
>trying to achieve here can already be achieved through polymorphism.
>
>I think you just need to redeisgn the classes a little bit.
>
>
I don't think that would (always) work. Consider the following:

interface IA
{
int get();
}

interface IB
{
int get();
}

class C : IA, IB
{
public int i;
// illegal but ...
int get() { return i+1; } // get for IA
int get() { return i+2; } // get for IB
}

void main()
{
auto obj = new C;
IA a = obj;
IB b = obj;

obj.i = 0;
writef(a.get, \n);	// should print 1
writef(b.get, \n);	// should print 2

obj.i = 2;
writef(a.get, \n);	// should print 3
writef(b.get, \n);	// should print 4

}

Both "a" and "b" are actually pointing to obj but calls to "get" using them are
supposed to differ. Some types of this problem could be handled by deriving
classes from C but not when the interfaces must actually be dealing with the
same object.

Cases like this could really happen if someone needs to implement two interfaces
from different libraries in the same class.





More information about the Digitalmars-d-learn mailing list