Weird opEquals Problem
Kevin
kevincox.ca at gmail.com
Wed Feb 22 17:51:50 PST 2012
I have the following code which gives the same result on ldc2 and dmd.
If I compare two objects of different classes I always get false even
though the comparator is called.
The code:
~~~~~~~~~~~~~~~~~~
import std.stdio;
class A
{
override bool opEquals(Object o)
{
writeln("In opEquals()");
return true;
}
}
class B
{
}
void main()
{
auto o1 = new A();
auto o2 = new A();
auto o3 = new B();
writeln(1);
assert( o1 == o2 );
writeln(2);
assert( o1 == o3 ); // line 26.
}
~~~~~~~~~~~~~~~~~~~~~
The output:
~~~~~~~~~~~~~~~~~~~~~
$ dmd -w test.d && ./test
1
In opEquals()
2
In opEquals()
core.exception.AssertError at test(26): Assertion failure
----------------
./test() [0x804b246]
./test() [0x8049f32]
./test() [0x8049902]
./test() [0x804b848]
./test() [0x804b355]
./test() [0x804b88f]
./test() [0x804b355]
./test() [0x804b304]
/lib/libc.so.6(__libc_start_main+0xf3) [0xb7527483]
----------------
~~~~~~~~~~~~~~~~~~~~~
The key thing to notice is that opEquals() gets called both times. Any
ideas about what is happening?
More information about the Digitalmars-d-learn
mailing list