[prog] C++ Problem

Robert J. Hansen rjh at sixdemonbag.org
Sun Apr 6 09:29:47 EST 2003


First, I'm going to be intermixing some style guidelines along with an
answer to what you're asking.  Please keep in mind these are not
absolute rules, and some people will disagree with my style
recommendations--and that's okay.

I'm also not going to give any examples of how I would do it.  Instead,
I'll refer you to the published literature, and give a few suggestions
for places where you might try and make your code a little more elegant.

Remember: I'm not slamming your code.  I'm pointing out some places
where you might want to play a little bit, and see what new things you
can learn.  That's all.

> using namespace std;

Style:

Avoid "using namespace std" in global code blocks.  The std:: namespace
has _lots_ of stuff in it, and you've got pretty good odds of a
namespace collision (when you name a variable, a function, a whatever,
the same as something that's already in the std:: namespace).  Yes, I've
been bitten by this one in the past.  Learn from my (many!) errors.  :)

See: Stroustrup, _The C++ Programming Language 3rd Edition_, chapter 8;
particularly 8.2.9, 8.4.8.

>     Vehicle a;
>     InitialiseVehicle(&a,"Audi");

Style:

Explicit constructors are generally thought of as being very C-ish.  C++
has very rich constructor mechanisms: try using them.  See if it makes
your code any terser, any easier to use, and your code easier to
program.

See: Stroustrup, Chap 10.  Particularly 10.2.3: "The use of [explicit
initializers] to provide initialization for class objects is inelegant
and error-prone.  Because it is nowhere stated that an object must be
initialized, a programmer can forget to do so--or do so twice (often
with equally disastrous results).  A better approach is to allow the
programmer to declare a [method] with the explicit purpose of
initializing objects."

>     PrintVehicle(a);

Style:

Providing an ostream<<(ostream &s, Vehicle &v) function allows you to do
output over anything that supports C++ streams (disks, screen, network
sockets, pipes, etc.).  This is an advanced C++ topic: come back to it
in a few months and see if you can apply it here.  Trust me, it's well
worth learning.  :)

See: Nicolai Josuttis, _The C++ Standard Library_ (better coverage of
streams than Stroustrup, IMO).

> void InitialiseVehicle (Vehicle *a, char name[20])
> {
>         a->vehicle_name, name;

Bug:

strncpy(a->vehicle_name, name, 19);

Alternate fix:

strcpy(a->vehicle_name, name);

(strncpy is overwhelmingly the preferred solution, though, and for good
reason.)

This is a pretty common class of bugs.  C-style string handling is a
headache even for veteran C/C++ programmers.  I know I've screwed up
string handling a few thousand times, and I've been coding in C++ for
fourteen years.


Style:

Using C-style char[] arrays, const char*s, or char*s to store strings is
considered bad form.  Look into using the std::string class instead.

See: Stroustrup, Chap 20, particularly 20.4.1: "Whenever possible,
C-style strings are best avoided in favor of [std::strings].  C-style
strings and their associated standard functions can be used to produce
very efficient code, but even experienced C and C++ programmers are
prone to make uncaught `silly errors' when using them."


Hope this helps.  If you've got any more questions, just holler.  :)
-- 
Robert J. Hansen <rjh at sixdemonbag.org>



More information about the Programming mailing list