Beginners are excited about a new language but C++ has one little feature that has always annoyed me, after years of programming, I make the same mistake, time and time again and sometimes, its difficult to find and when I do find it, I get so ticked off.
Look at the following code and tell me what is wrong...
This compiles perfect fine but the expression always evalutes to true and sets number to 3 making a bug almost impossible to find if this little if statement was inside a program with thirteen-thousand lines of code...yikes!
The correct way is
if (number==3) sum=4;
That is how its always done but yet, the potential to trip and fall is there, the = and == operator is a banana skin waiting for you some where on the floor, just when you least expect it, you slip and hit your head.
The text editor I wrote under DJGPP long ago, had a bug like this, when I found the bug after 2 weeks of search and testing, I was furious, it dind't make any sense.....it was at that time that I sat down and did the following...
#define is ==
I went arund every were replacing the == operator with is and my code ended up looking something like...
if (number is 3) sum = 4;
Altho this was probably a very bad idea it made for some rather interesting looking code, in fact I posted the code on a forum way back and people were wondering what wacked version of C++ I had, until I uncovered the #define for is which was converted to == during compile time.
I eventually dropped the habit but when I make the mistake it makes me wonder, why didn't they just use another keyword for the C++ language, drop == altogether and make it "is" which, could stomp the dangerous camoflouge bug that hides itself from view. I know it is a bad idea to add another keyword to the language but you must admit = and == ... people make that mistake on ocassion if they aren't careful and when they do, it could set back the development of a project for one little "typo".
I seriously believe, they need to do something about it, i'm interested in hearing what everybody else has to say about this. Have you had it happened to you?