473,408 Members | 2,888 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

null and overloading == operator

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 check for a null
reference within that implementation cuz it gets recursively called
resulting in a stack overflow.
operator overloading is still rough waters for me. mostly worked in java
so never had that option.
appreciate any help, thx.
A.
Nov 16 '05 #1
3 2736
Andrew Gniadek <ag******@hotmail.com> wrote:
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 check for a null
reference within that implementation cuz it gets recursively called
resulting in a stack overflow.
operator overloading is still rough waters for me. mostly worked in java
so never had that option.


Either cast one or both sides to null to get identity comparison, or
use Object.ReferenceEquals.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
You could put it in a try..catch block and just catch the
NullPointerException that occurs if you reference the null.

Andrew Gniadek wrote:
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 check for a null
reference within that implementation cuz it gets recursively called
resulting in a stack overflow.
operator overloading is still rough waters for me. mostly worked in java
so never had that option.
appreciate any help, thx.
A.


Nov 16 '05 #3
Bryan <Bi********@centurytel.net> wrote:
You could put it in a try..catch block and just catch the
NullPointerException that occurs if you reference the null.


Yikes - while I'm not one of those who advocates avoiding exceptions at
all costs, that's a really horrible way of doing things. There are much
better solutions in this case, just as there's almost *always* a better
solution to avoiding a NullReferenceException than just catching it.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: | last post by:
Hi all, I've been using C++ for quite a while now and I've come to the point where I need to overload new and delete inorder to track memory and probably some profiling stuff too. I know that...
9
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) {
3
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...
14
by: juanph31 | last post by:
Hello there, Why is it that I sometimes see this construct //case 1 if ( null == myVariable) { //...etc }
10
by: Wilhelm Heramb | last post by:
What is the best practice to implement operator overloading for == and != that handles null on either lhs or rhs. Andreas :-)
5
by: luca regini | last post by:
I have this code class M { ..... T operator()( size_t x, size_t y ) const { ... Operator overloading A ....} T& operator()( size_t x, size_t y )
2
by: brzozo2 | last post by:
Hello, this program might look abit long, but it's pretty simple and easy to follow. What it does is read from a file, outputs the contents to screen, and then writes them to a different file. It...
3
by: y-man | last post by:
Hi, I am trying to get an overloaded operator to work inside the class it works on. The situation is something like this: main.cc: #include "object.hh" #include "somefile.hh" object obj,...
2
by: Colonel | last post by:
It seems that the problems have something to do with the overloading of istream operator ">>", but I just can't find the exact problem. // the declaration friend std::istream &...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.