c plus plus
... random thoughts , programming
sun 2005-apr-17 15:54:39 pdt
So one not-really-anticipated side-effect of being back in school now
(as I have been the last six months) after seven years in industry is
that I have to write C++ code again.
The last time I actually wrote any C++ was in my first job out of
college, at Apple, where I worked very briefly on handwriting
recognition for the Newton. Since then I've written mostly Java, a
fair bit of Python, some Lisp and other languages, and a surprising
amount of Bourne shell -- you'd be surprised just how large and
complex a system can be built entirely in /bin/sh. Well, okay,
actually, the surprising part is not how large and complex a system
can be built entirely in /bin/sh, it's more how large and
complex a system someone (mostly me) has actually gone ahead and built
entirely in /bin/sh. But that's a blog entry for another day,
Anyway, so now I'm having to use C++ again after all these years.
I've noticed a few things about it, and they're not really the same
things that a few months ago I would have cited as my reasons for
preferring Java or Python over C++. I mean, sure, the memory
management thing is kind of annoying, but the biggest pain, I've
discovered, is keeping track of pointers, and references, and when
you're implicitly calling constructors and whatnot...
So for example, the other day I needed to pass an array to a function
and have that function move around the elements within the array. The
elements themselves were pointers. So I've got this const Foo
*myArray, right? My first thought was to pass it by reference,
but, well, a reference to an array of pointers seemed silly, so I
thought I'd go with a pointer to an array of pointers, by having the
function argument be const Foo ***myArrayPtr. But then when
I called the function (passing it &myArray), I couldn't
get that to compile. After talking about it with some friends who've
written way more C++ than me in the last five years, I realized
that I didn't need a pointer to an array of pointers, I could just
pass the array of pointers itself (since that's really just a
pointer), and be done with it. That fixed it, of course, but man,
what a pain, having to think about these things.
Another thought I had was that it was nice to have operator
overloading back. I totally understand why the Java folks made the
decision to leave it out, and I would even go so far as to say that
they were right to do so. But nevertheless, it sure is a nice feature
to have! When I was first learning Java, I remember thinking that
gaining inner classes somehow made up for losing operator overloading
-- not that the one would take the place of the other for
accomplishing any particular task, but just, as language features go,
it felt like they'd taken one thing away and given me something else.
At the time, I think it seemed about an even trade, but now I don't
really want to trade back -- I think maybe I'd rather have inner
classes. I mean, on the one hand, the C++ stuff I've done lately
hasn't really needed them, while it has made use of operator
overloading. But on the other hand, it does feel like inner classes
fundamentally provide something new, whereas operator overloading,
nice as it is, is really just syntactic sugar.
But hey, sugar's better than no sugar.