How to Compare 2 objects of the same class

Basile B. via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Jun 3 13:23:38 PDT 2017


On Saturday, 3 June 2017 at 20:02:13 UTC, Mark wrote:
> On Saturday, 3 June 2017 at 19:57:47 UTC, Mark wrote:
>> Hello again.
>>
>> I'm designing a template version of a BST.
>>
>> Because of this, I want to be able to compare if two objects 
>> of the same class type are references to the same anonymous 
>> class on the heap somewhere.
>>
>> Example:
>
> Not sure what happened there.
>
> Anyways, example:
>
> auto A = new Box();
> auto B = new Box();
>
> if(A.opEquals(B)) {}
>
> gives the error
> test.o:(.data.rel.ro+0x18): undefined reference to 
> `_D5Stack12__ModuleInfoZ'
> collect2: error: ld returned 1 exit status
> Error: linker exited with status 1
>
> if(Object.opEquals(A,B) gives
>
> test.d(10): Error: function object.Object.opEquals (Object o) 
> is not callable using argument types (Box, Box)
>
>
> How do I got about comparing two objects then??
> Thanks.

I think that the error is unrelated. Which dmd do you use ?

Otherwise, opEquals (or just == ) is well the way to follow. By 
default Object.opEquals just compare the Objects addresses. If 
you want to perform a deep comparison (i.e the members) you have 
to override opEquals.


More information about the Digitalmars-d-learn mailing list