[geeks] C++ style

Jonathan C. Patschke jp at celestrion.net
Thu Sep 19 12:58:22 CDT 2002


On Thu, 19 Sep 2002, Joshua D Boyd wrote:

> In my life, I want to benefit from "modern" classes to aid life, like
> say dictionaries.

Dictionaries, hashtables, dynamic arrays, trees, and the like?

> STL has many of these classes, but for various reasons, it isn't
> practical for me to rely on them.

Practical meaning "you don't have access to ANSI C++ runtime libraries"?

> The only thing I can safely rely on is a set so small that they can
> easily fit on a floppy with lots of space to spare, and additionally,
> work with, at a minimum VC++ and GCC.

VC++ and G++ both support a fairly common subset of STL.  Also, why are
you limiting yourself to floppies?  So you want an SSH account on my
server so that you can at least use sftp or http (I can write an
upload/download app quickly) to store your files?

> So, having not found any that I like, I find myself writing my own
> little classes, and constantly copying them and changing them for the
> project at hand.

Sounds like my current dilemma with C.  Every program I start has me
reimplementing a hash table and a doubly-linked list.  I'm loathe to
make my own library, because I don't want Yet Another Dependency, and I
don't want to commit to glib because it's (L)GPL and I don't want YAD.

> Is it a good idea to write a an array class or dictionary class that
> maintains an array or dictionary of void*s,

ACK!!!!  C++ libraries should -never- use (void *) in public interfaces.
You're thinking in terms of C, not C++.  You're giving up the -huge-
benefits of C++'s type-checking.  The whole point of the STL is that if
you create an array that's supposed to hold TextFields, you can only put
TextFields into it, and when you get things from it, they're
automagically typecast to TextFields by the compiler.

> or should I suck it up and create my own base class system,

Ack!  Duplicated effort alert!

> or should I break down and use templates (despite all the complaints I
> hear about them being slow, and the fact that I'm working on code that
> needs to be vaguely performance oriented).

Templates aren't slow at all.  Most of the fancy footwork happens at
compile-time.  At runtime, you just have dynamic binding to consider,
which you'd have to deal with in C++ anyway.  Templates will save you a
-lot- of work in this case.

> I like the idea of using void*s, but when I was commenting on it to one
> student, he said it sounded like a dirty hack.

Of course you like the idea of using (void *)--it's reminiscent of a
more sane time, when C ruled the earth and Stroustrup was still trying
to pick up sorority sisters.

> But, I'm not sure he really sees the value in the idea of maintaining
> a stack of personal classes for personal use.

I'm not sure -I- see the value when the STL is nearly omnipresent in
current versions of most compilers.  Hell, -MIPSpro- is getting their
act together in the next maintenance release.

> for everything, but that isn't really realistic with my current skill
> set (or rather lack there of).

Pick up a copy of Bjarne's book, read about class templates, and
meditate on it for a while.  Things will become clearer, and you won't
even remember how you wrote C++ code without them.  Templates are a Good
Thing, even if the syntax for expressing them sucks rocks.

-- 
Jonathan Patschke
   > Can you SysAdmins tell me what might go on in a typical day?
   Hours of endless frustration punctuated by moments of sheer terror.
                                 --Saul Tannenbaum (in the Monastery)



More information about the geeks mailing list