Comparing apples to oranges, or strings to bools
-
I spent this afternoon trying to figure out why my code was throwing an exception. I tracked it down to the line
if (strProtocol == FALSE)
which should have been
if (strProtocol.IsEmpty() == FALSE)
So why did the original compile in the first place? Because
FALSE
is #defined as 0, and coincidentally so isNULL
, and our string class has anoperator==(const char*)
— which does not properly check forNULL
. -
I spent this afternoon trying to figure out why my code was throwing an exception. I tracked it down to the line
if (strProtocol == FALSE)
which should have been
if (strProtocol.IsEmpty() == FALSE)
So why did the original compile in the first place? Because
FALSE
is #defined as 0, and coincidentally so isNULL
, and our string class has anoperator==(const char*)
— which does not properly check forNULL
.I was caught out with a very similiar thing. The feedback i got was that what you are doing is comparing the objects are the same object (i.e. they reference the same thing) as oppossed to comparing the objects state. So the compiler is happy. Now whether that is right or not is another question for someone smarter than me!
Dave Who am I?: http://www.bebo.com/daveauld/ or http://www.dave-auld.net/
-
I was caught out with a very similiar thing. The feedback i got was that what you are doing is comparing the objects are the same object (i.e. they reference the same thing) as oppossed to comparing the objects state. So the compiler is happy. Now whether that is right or not is another question for someone smarter than me!
Dave Who am I?: http://www.bebo.com/daveauld/ or http://www.dave-auld.net/
I think the problem comes down to the fact that the value of FALSE is evaluated by the preprocessor (I'm assuming this is C++) so the actual code compiler sees
if (strProtocol == 0)
which is a valid statement. The programmer distinguishes between NULL and FALSE, the compiler doesn't. -
I spent this afternoon trying to figure out why my code was throwing an exception. I tracked it down to the line
if (strProtocol == FALSE)
which should have been
if (strProtocol.IsEmpty() == FALSE)
So why did the original compile in the first place? Because
FALSE
is #defined as 0, and coincidentally so isNULL
, and our string class has anoperator==(const char*)
— which does not properly check forNULL
.It sucks that the correct solutions are always rather complex[^]
Personally, I love the idea that Raymond spends his nights posting bad regexs to mailing lists under the pseudonym of Jane Smith. He'd be like a super hero, only more nerdy and less useful. [Trevel]
| FoldWithUs! | sighist