How to Compare 2 objects of the same class

H. S. Teoh via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Jun 3 22:43:05 PDT 2017


On Sat, Jun 03, 2017 at 10:38:31PM +0000, Mark via Digitalmars-d-learn wrote:
[...]
> Ok. So by using '==' it should compare the addresses of the objects?
[...]

No, `==` is for comparing the *contents* of the objects.  You need to
implement opEquals() for the objects being compared in order to define
how the contents will be compared.

If you want to compare *addresses*, use `is`:

	Object a = new Object(...);
	Object b = a;	// b is a reference to the same object as a

	assert(b is a);

In this case there is no need to implement anything else, since
comparing addresses is simple.


T

-- 
"No, John.  I want formats that are actually useful, rather than over-featured megaliths that address all questions by piling on ridiculous internal links in forms which are hideously over-complex." -- Simon St. Laurent on xml-dev


More information about the Digitalmars-d-learn mailing list