Re: What would be considered "unusual" C++ code?
RyanMcCoskrie wrote:
I've been coding alone on pet projects for a while now and my code is
beginning to develop
eccentricities that run against some features in C++. Would any of
this be considered normal?
0: Using fprintf( stderr ...) instead of std::cerr
You really really shouldn't do that (s|f|)printf isn't
typesafe and these kind of errors can show up under very
subtle conditions. Consider the following:
<snip>
const char * s ="%d\n";
printf(s,3L);
</snip>
This works perfectly for me, because sizeof(long)==sizeof(int)
on my platform. But I can't move it over to another one
where that's not the case.
Formatting stuff on an ostream is much more verbose and storing/restoring
the current state can be a real pain in the neck. It would be nice to
have more standardized functions/operators for things like this.
But on the other hand you're able overwrite the << operator hides
complexity of object output from you.
0: Making use of goto statements instead of brief functions (though I
do use this carefully).
Uhh -- I will not comment on that. It's a looong discussion and if
even godfather knuth can demonstrate that in some circumstances it's
preferable to use goto, who am I to reject it all together.
0: wrapping up large chunks of program behavior in a namespace
to avoid a class that is used only once.
I think there's nothing wrong with classes that are only used once.
Even with object instances of such classes. First of all think of a
class as a namespace with additional properties:
a. It allows access control via private,public,protected
and inheritance
b. It may work on a "hidden" chunk of data. If this is meant to
be static for the whole program you call it static for the
class (and still don't need an instance). If you need
multiple instances you need object instances of his class.
So classes as namespace replacements make sense for me.
0: avoiding loops other than while.
Never heard that that for() {} loops are supposed to
be bad. They tend to become boring when you have to code
<snip>
for(C::ierator i=c.begin();i!=c.end(),i++)
</snip>
all over the place. But I guess that's the reason
the for_each template was born.
0: Using C style strings where ever possible.
No -- only use it where it isn't avoidable. This one is
the reason for many many stack overflow errors, injection
of malicious code etc... It'll blow up your local
power plant.
I still wonder why the STL still employs it at many places
without even alternative calls accepting std::string &
e.g:
there is no std::fstream(std::string &)
but
there is no std::fstream(const char *)
or
codecvt<...>::in
etc..
But avoid it wherever you can.
If people have good reasons that these practices are to be stopped I
will take their counsel
and I would like to know what other odd things people have seen or do
as well.
My 0.06???
O.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]