When is Unicode not Unicode? When Microsoft gets involved!

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft have been lying to you about what constitutes Unicode? They will have you believe that Unicode requires you to use a WCHAR (wide) character type and that Unicode cannot be represented by a CHAR (narrow) character type. In fact, both of these statements are completely and utterly false. Microsoft has misled you in the most egregious way. Continue reading

C / C++ main function prototypes

There seems to be some fundamental misunderstanding about the function prototype for the “main” function in C and C++. More specifically what type this function should return. I see so many programmers use void as the return type. People, I’m sorry to tell you but that’s just plain wrong! Continue reading

C++ Catching exceptions in constructors

When deciding whether to catch the exception, especially one thrown during construction, ask yourself a very simple question, if you do catch it can you still leave the class in a usable state? If the answer is yes, then by all means catch it, deal with it and allow construction to continue. If the answer is no then do not catch it! Why? Simple! if you catch and swallow it, how is the code that was attempting to construct your class to know that the class is unusable (or, at least only partially constructed)? Continue reading

C++ Throwing exceptions from destructors

You’re implementing a beautiful class. It’s just wonderful – the most perfect code you’ve ever written, except… for some reason it keeps crashing and you don’t know why. You’ve debugged the code and discovered it happens when your class goes out of scope and needs to throw an exception from the destructor. Tested in isolation, this works fine but integrated into the main code base it causes a crash. You know there are exception handlers that should deal with this and yet it still crashes. This doesn’t make sense. Why is the crashing? Continue reading

C++ Passing by value vs. passing by reference

In C++ all arguments are passed to functions by value. In other words, a copy of the value is taken and that is what the function has to work with. If you want to modify the original value you must either pass a pointer or reference to the original value you wish to modify. Of course, even the pointer or reference are still passed by value. Pointers and references are just another C++ type and when they are passed around you do so by value. Continue reading

Should I learn C before learning C++? Answer – NO!

C is Not C++!!!

A superset of nothing useful

I often hear those who really should know better giving the advice that before you learn to code in C++ you should first learn to code in C. At face value this would seem like reasonable advice; after all C++ is a superset of C and so by learning C you’ll be learning some of C++. Unfortunately, this advice overlooks some fundamental but very important differences between C and C++ that may very well damage the learning curve of the student. Continue reading