interface and opEquals
Oliver
oliver.ruebenkoenig at BLUBweb.de
Tue Nov 6 00:00:26 PST 2007
Steven Schveighoffer Wrote:
Steve,
thank you for looking into this.
> You should use Object as the argument. The reason is simple. If you do not
> use Object, then the base opEquals function is not overridden. When you use
> Vi as your argument, you start a new method that is overridden only by
> derivatives of Vi.
That puts it very clearly.
> Your example isn't a good way to demonstrate why this is
> bad, but imagine if Vi had a base interface...
Unfortunately, I do not understand this. Could you explain a little more, or tell me were i could read up on this?
> Having said that, you do not need opEquals to be defined as a method in your
> interface, because Object already defines it. All classes derived from
> Object will have a valid opEquals. Perhaps this is why it was not working
> for you.
> I think this code would work:
>
The second assert fails. And that is the one i would like to work. I have the impression that it does not work because opEquals does not know what to do with Vi objects.
Then i tried to have opEquals( Object ) in the interface and wanted opEquals ( C1 test ) in the C1 class. But that did not work either. Which kind of makes sense.
hm, i do not really understand this. Any ideas?
Thanks,
Oliver
> import std.stdio;
>
> interface Vi {
> void f();
> //int opEquals( Vi ); // ** commented out
> //int opEquals( Object );
> //int opEquals();
> }
>
> class C1 : Vi {
> void f() { writefln( "C1: ", itsInt ); }
> this( int i ) { this.itsInt = i; }
> int opEquals( Object o ) { // ** changed to Object
> C1 test = cast(C1)o;
> return test && this.itsInt == test.itsInt;
> }
> //int opEquals( C1 test ) { return this.itsInt == test.itsInt; }
> private:
> int itsInt;
> }
>
> int main() {
> assert( 1 == 1.0 );
> Vi myInt1 = new C1(1);
> Vi myInt2 = new C1(1);
> myInt1.f();
> assert( cast(C1)myInt1 == cast(C1)myInt2 );
> assert( myInt1 == myInt2 );
> return 0;
> }
>
> -Steve
>
>
More information about the Digitalmars-d-learn
mailing list