C++ versus Java, or references to references versus values of references.
A few years of Java really make it hard to write C++ again. Quick, which language is this?
That works in Java and C++ but for different reasons. It's usually safer in C++.
In Java that code is less safe because it hasn't been tested for null. That is, one of the following already happened:
- Stuff stuff = null;
- Stuff stuff = new Stuff();
- Stuff stuff = Stuff.class.newInstance();
C++ gives you stronger references. For that same line of code, one of these already happened:
- Stuff stuff;
- Stuff *stuff_p = new Stuff(), &stuff = *stuff_p;
- Stuff& f2() { Stuff stuff; return stuff; }
- Thisrefers to an object allocated on the stack. It's there, baby -- as much as anystufforint.float
- Thisrefers to an object successfully allocated in the heap.stuff
- Very bad.destructed the stuff before giving it to you. Just keep compiler warnings on and it will bark "warning: reference to local variable 'stuff' returned"f2
What wasn't shown for C++ was
- Stuff *stuff;
- Stuff *stuff = null;
- Stuff *stuff = new Stuff();
- std::auto_ptr<Stuff> stuff(new Stuff());
- Uninitialized pointer, a.k.a. Runtime Russian Roulette. Unfortunately I couldn't get g++ to whine about this even with.-Wall -Wpedantic -Wextra
- Null pointer, a.k.a. the minimum guarantee of Java.
- Hmm, I don't see a delete any where around here... uh-oh...
- There you go. You've got a smart pointer on the stack to prevent memory leaks, while preserving the expectedsyntax.->
So WHAT? Really now!
Java guarantees that a reference will be initialized, and that it must always be tested for null. C++ warms the hearts of bearded old curmudgeons with more types of references. Each type is useful alone and in sometimes in conjunction (e.g. non-const references to pointers for output parameters). This is useful and important information to know. Draping Object everywhere and automating garbage collection is a lowbrow kludge.
Footnote 1: Groovy's safe navigation operator helps test for null by GOTOing the next safe expression. An interesting idea, and hopefully self-preventative. If I saw code with lots of '?' it would be up for a rewrite sooner rather than later.
Resolving to use C++ in 2009
2009 is almost here and it's time to start thinking about resolutions. Everyone knows that resolutions are honored more in the breach than the observance. Keeping that in mind, here are my presolutions for 2009:
A.) I hereby resolve that 2009 will be the year of C++ for my coding hobby.
... and ...
B.) I resolve to forget about this resolution as soon as my attention span wanes.
I'm justifying this lack of commitment by calling these pre-resolutions or "presolutions" for short. It's only December 2008, I don't see any problems with that.
I haven't used C++ since 2005; back then exceptions and Boost were verboten in the name of cross-platform compatibility. It was a little annoying but understandable; we had to appease C++ compilers from Microsoft, IBM, Sun, GNU, Metrowerks, and others. Hacks appeared all the time... sometimes you just wanted to ignore code rejected by the excellent Comeau online C++ compiler. But actually shipping things means living with warts sometimes.
Back in 2005 I had trouble imagining that Java would survive. I watched the misbegotten offspring of C++ and Visual Basic nearly reimplement CORBA in EJB2 and cackled in glee. Demise was surely imminent.
I've been using Java almost exclusively since 2005. Joke's on me!!
Since then I have been afraid that C++ was fading away into obsolescence. TIOBE says I'm wrong. All those trendy important new programs like "web browsers" not implemented in dynamic languages seem to concur.
Yes, these demonstrate my complete inability to predict trends in my occupational field. Go ahead and snicker, I deserve it. I'm like King Midas in some alternate universe where things I don't touch turn to gold.
So it is with some trepidation that I once again cast my befouling gaze on my fond old friend C++. I can change, things will be different this time! I won't pine over the preprocessor or pointer arithmetic. Honest!
I think I have a lot of catching up to do.
- First, I have to readjust my perspective on coding to include manual memory management, macros, virtual destructors, and avoiding non-const references to temporary objects.
- Next, I need to come up to speed on the standard template library (STL) and resource acquisition as initialization (RAII).
- Not sure how much I need to worry about C++0x.
Quite a lot of work ahead. At least I get to use GNU make again. Seriously. No snickering or eyerolling allowed!
This post is just my attempt to motivate myself. Following posts will (hopefully happen and) be substantial.


