Sunday, January 08, 2006

How do I Love thee C++? Let Me Count the Ways

Here is one:

While casually glancing at the CORBA C++ bindings, I read about the very nice wrapper class String_var.

It has all the nice features of String classes and smart pointers.

It, of course, has overloaded constructors for char * and const char *. Only one of them (char *) takes over the pointer and frees it in its destructor. You wouldn't want anyone doing a free("hello world"), Which is why you need the two constructors.

So?

Well... as we all know, in C++ the literal "hello world" is a char *, not a const char *. Since everybody knows that, nobody would make the following mistake:

String_var s("hello world");

Not a very common idiom. Most people would instead prefer to write:

String_var s((const char *)"hello world");

Which is a much more natural way to program anyway.

In perspective this is not so bad though. If you take into account the millions of programmers around the world, who write millions of lines of multi-threaded code, which zip along at 800 Mhz on millions of computers worldwide.

It isn't so bad because most of these run on top of Microsoft Windows, which was written by an equally qualified group of programming professionals (in C++ of course).

Each one of these highly qualified professionals of course knows the difference between, for example:

char *cp = "hello world";

and

char cp[] = "hello world";

It's OK though, as long as we can reboot the machine... and nobody's pacemaker is connected to it via a multithreaded USB interface patched with service pack 3.45.9A...

Grace Hopper: where did you go wrong...