What is the best practice to implement operator overloading for == and !=
that handles null on either lhs or rhs.
Andreas :-) 10 2182
Do you mean "Should I handle null on either lhs, or rhs?". Answer: do
whatever the Object "==" operator does (it handles null on either side).
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
etc..etc..
Anything else could go wrong, or could confuse a reader.
"Wilhelm Heramb" wrote: What is the best practice to implement operator overloading for == and != that handles null on either lhs or rhs.
Andreas :-)
Hi Andreas,
First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need to the best practice for
operator overloading. If there is any misunderstanding, please feel free to
let me know.
To overload operators that handles nulls, you need to check for null in the
operator method. You can take a look at the following link for tutorial on
operator overloading. http://msdn.microsoft.com/library/de...us/csref/html/
vcwlkoperatoroverloadingtutorial.asp
HTH.
Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
Thanks for the support guys :-)
The following does not work if ie. rhs is null;
public static bool operator ==(PreparePhone lhs, PreparePhone rhs)
{
if(lhs.Number == rhs.Number && lhs.NumberType == rhs.NumberType)
return true;
else
return false;
}
public static bool operator !=(PreparePhone lhs, PreparePhone rhs)
{
return (lhs.Number != rhs.Number || lhs.NumberType != rhs.NumberType);
}
public override bool Equals(object phone)
{
return ((this.Number.Equals(((PreparePhone)phone).Number) )
&& (this.NumberType.Equals(((PreparePhone)phone).Numb erType)));
}
"Javaman59" <Ja*******@discussions.microsoft.com> wrote in message
news:CA**********************************@microsof t.com... Do you mean "Should I handle null on either lhs, or rhs?". Answer: do whatever the Object "==" operator does (it handles null on either side).
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
etc..etc..
Anything else could go wrong, or could confuse a reader.
"Wilhelm Heramb" wrote:
What is the best practice to implement operator overloading for == and != that handles null on either lhs or rhs.
Andreas :-)
Hi Kevin.
cute sig, btw.. :)
You might be interested to know that the link doesn't work for me (and
probably anyone outside the MS VPN)
"Kevin Yu [MSFT]" wrote: Hi Andreas,
First of all, I would like to confirm my understanding of your issue. From your description, I understand that you need to the best practice for operator overloading. If there is any misunderstanding, please feel free to let me know.
To overload operators that handles nulls, you need to check for null in the operator method. You can take a look at the following link for tutorial on operator overloading.
http://msdn.microsoft.com/library/de...us/csref/html/ vcwlkoperatoroverloadingtutorial.asp
HTH.
Kevin Yu ======= "This posting is provided "AS IS" with no warranties, and confers no rights."
Hi,
Please pay attention to the word wrap. The link contains 2 lines.
Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
Kevin Yu [MSFT] wrote: Hi,
Please pay attention to the word wrap. The link contains 2 lines.
Actually, on my machine, the (broken) link contains 1 line, it would
appear that the other line is a broken part of the link
The proper link might be
<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vcwlkoperatoroverloadingtutorial.asp>
JB ;)
Kevin Yu ======= "This posting is provided "AS IS" with no warranties, and confers no rights."
"Javaman59" <Ja*******@discussions.microsoft.com> wrote in message
news:CA**********************************@microsof t.com... 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
James,
I think you're right in that the code as it stands produces an infinite
loop.
I have seen somewhere the problem solved this way:
object ol = l, or = r;
if (ol == null || or == null) // here the System.Object operator == is
used
return false;
else {
// here the "real" comparison
}
Regards - Octavio
"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
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" wrote: "Javaman59" <Ja*******@discussions.microsoft.com> wrote in message news:CA**********************************@microsof t.com... 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
Oops! I did it again! :-)
The correct version (I hope) is:
object ol = l, or = r;
if (ol == null)
if (or == null)
return true;
else
return false;
else
if (or == null)
return false;
else {
// compare "l" and "r"
}
Thanks for pointing it out.
Regards - Octavio
"Javaman59" <Ja*******@discussions.microsoft.com> escribió en el mensaje
news:C6**********************************@microsof t.com... 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" wrote:
"Javaman59" <Ja*******@discussions.microsoft.com> wrote in message news:CA**********************************@microsof t.com... > 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: |
last post by:
I have not posted to comp.lang.c++ (or comp.lang.c++.moderated)
before. In general when I have a C++ question I look for answers in
"The C++ Programming Language, Third Edition" by Stroustrup....
|
by: Mohammad |
last post by:
I'm implementing a c++ template class CScan to minipulate a series of
numbers.
I have implemented operator() to select a range of numbers it works
fine for an expression like:
scan1 = scan2(0,...
|
by: ganesh.tambat |
last post by:
Hi,
Please see below a piece of code. Here I am trying to create a linked
list by attaching two linked list together. I have overloaded operator
+ for this. Now the output always says that the...
|
by: Steve Sargent |
last post by:
Hi:
I'm trying to debug the following code, and it keeps looping on the if
statement:
public static bool operator ==
(OnlineMemberNode first, OnlineMemberNode second)
{
if(first == null)
{
|
by: Andrew Gniadek |
last post by:
hey,
if i overload the == operator then i have problems with checking if a
reference to an object is null. just wondering how i can get away with
it. because as soon as I overload it then i can't...
|
by: Suresh Tri |
last post by:
Hi,
I was trying to overload concat operator ||(text,text) such a way that
it behaves like Oracle. i.e. I want 'abc' || null to return 'abc'
instead of null.
I know that it is not the expected ...
|
by: TuxC0d3 |
last post by:
Hi!
I'm diving into the some more ++ specific aspects of c++ (and finally
accepting that c++ is more than "a plus added to c" :), so that means
using namespaces, templates, std::strings, lists,...
|
by: citystud |
last post by:
I am trying to convert the c++ code to C#, but find difficulty to convert the overloading operator. Here is the source code.
I don't really know how to implement the operator = and operator () in...
|
by: xkenneth |
last post by:
Hi,
I'm writing a sparse matrix class for class and I cannot seem to get
operator overloading to work properly.
I've overloaded an operator with the code here.
matrix operator +(matrix one,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
| |