W
Wilhelm Heramb
What is the best practice to implement operator overloading for == and !=
that handles null on either lhs or rhs.
Andreas
that handles null on either lhs or rhs.
Andreas

Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.



Actually, on my machine, the (broken) link contains 1 line, it wouldKevin said:Hi,
Please pay attention to the word wrap. The link contains 2 lines.

Javaman59 said:Or do you mean "How do code it?". I'd be a bit surprised it this isn't
obvious, but perhaps you are feeling tempted to avoid the brute force method.
Just use the brute force method...
if (l == null)
if (r == null)
return true;
else
return false;
else
James Curran said:Javaman59 said:Or do you mean "How do code it?". I'd be a bit surprised it this isn't
obvious, but perhaps you are feeling tempted to avoid the brute force method.
Just use the brute force method...
if (l == null)
if (r == null)
return true;
else
return false;
else
The problem with that is, since this is being done within an overloaded
operator==, "if (l == null)" results in an infinite loop.
What one needs to do is:
if (Object.ReferenceEqual(l, null))
// etc
--
--
Truth,
James Curran
[erstwhile VC++ MVP]
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com

Javaman59 said:Thanks James & Octavio.
I've tested my code, and indeed it does produce infinite recursion. For a
solution, I prefer the (object) conversion to ReferenceEqual, just because
we
do a few of them, and its shorter.
I notice that Octavio's example returns false for == if both sides are
null.
I don't think this is correct, and is not consistent with the
System.Object
== operator.
This works...
class MyInt
{
int myValue;
public static bool operator ==(MyInt lhs, MyInt rhs)
{
// If either side is null, then only return true if
// both sides are null.
object ol = lhs;
object or = rhs;
if ((ol == null) || (or == null))
return ((ol == null) && (or == null));
else
// lhs & rhs not null. Compare instance values.
return lhs.myValue == rhs.myValue;
}
... plus a couple of functions required by the compiler
}
James Curran said:Javaman59 said:Or do you mean "How do code it?". I'd be a bit surprised it this isn't
obvious, but perhaps you are feeling tempted to avoid the brute force method.
Just use the brute force method...
if (l == null)
if (r == null)
return true;
else
return false;
else
The problem with that is, since this is being done within an
overloaded
operator==, "if (l == null)" results in an infinite loop.
What one needs to do is:
if (Object.ReferenceEqual(l, null))
// etc
--
--
Truth,
James Curran
[erstwhile VC++ MVP]
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.