Thread overview | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
June 03, 2017 How to Compare 2 objects of the same class | ||||
---|---|---|---|---|
| ||||
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: |
June 03, 2017 Re: How to Compare 2 objects of the same class | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mark | 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.
|
June 03, 2017 Re: How to Compare 2 objects of the same class | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mark | 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.
|
June 03, 2017 Re: How to Compare 2 objects of the same class | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mark | 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.
Could you please systematically post compilable code? It is very hard to say anything without even seeing what you're struggling with.
|
June 03, 2017 Re: How to Compare 2 objects of the same class | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mark | On 06/03/2017 10:02 PM, Mark wrote: > 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 Your code works for me (when adding `class Box {}` and a `main` function). An "undefined reference" error can mean that you forgot to include a module on the command line. > if(Object.opEquals(A,B) gives > > test.d(10): Error: function object.Object.opEquals (Object o) is not callable using argument types (Box, Box) Yeah, that's not how you do it. > How do I got about comparing two objects then?? > Thanks. Just use the usual equality operator: `==`. Or if you want to check for identity, use the binary `is` operator. ---- class Box {} void main() { auto A = new Box(); auto B = new Box(); if (A == B) {} if (A is B) {} } ---- By default, they act the same. But you can change how `==` behaves by overriding `opEquals`. You cannot override `is`. |
June 03, 2017 Re: How to Compare 2 objects of the same class | ||||
---|---|---|---|---|
| ||||
Posted in reply to ag0aep6g | On Saturday, 3 June 2017 at 20:24:44 UTC, ag0aep6g wrote:
> By default, they act the same. But you can change how `==` behaves by overriding `opEquals`. You cannot override `is`.
Ok. So by using '==' it should compare the addresses of the objects?
I think I didn't include the other file as an argument on the command line (Oops!)
I'm using dmd v2.074.0.
In the future I'll include a compilable example. I was having problems with a class I made which is about 45 lines, that might be a lot of code for a post.
But it seems to work now. This is good, I can have the comparisons work for any type without doing backflips.
Thanks.
|
June 03, 2017 Re: How to Compare 2 objects of the same class | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mark | On Saturday, 3 June 2017 at 22:38:31 UTC, Mark wrote:
...
> Thanks.
Actually, I got another question,
how Can I obtain the actual memory address of a class?
I'll still need to solve the problem of taking ints/floats/reals etc., as well as structs and classes and sending them right/left in the BST. :/
Thanks again.
|
June 03, 2017 Re: How to Compare 2 objects of the same class | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mark | On Saturday, 3 June 2017 at 22:38:31 UTC, Mark wrote: > In the future I'll include a compilable example. I was having problems with a class I made which is about 45 lines, that might be a lot of code for a post. You can use external resources such as: https://d.godbolt.org/ https://dpaste.dzfl.pl to paste code there and then just post a link to a paste here. |
June 03, 2017 Re: How to Compare 2 objects of the same class | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mark | On Saturday, 3 June 2017 at 22:52:42 UTC, Mark wrote:
> Thanks again.
Nevermind, I got it.
|
June 03, 2017 Re: How to Compare 2 objects of the same class | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stanislav Blinov | On Saturday, 3 June 2017 at 22:53:51 UTC, Stanislav Blinov wrote:
> On Saturday, 3 June 2017 at 22:38:31 UTC, Mark wrote:
>
>> In the future I'll include a compilable example. I was having problems with a class I made which is about 45 lines, that might be a lot of code for a post.
>
> You can use external resources such as:
>
> https://d.godbolt.org/
> https://dpaste.dzfl.pl
>
> to paste code there and then just post a link to a paste here.
I'll keep that in mind, thanks.
|
Copyright © 1999-2021 by the D Language Foundation