Re: extending c++ classes and enumerations
* perrog@gmail.com:
// File enum.h
enum ThreadState { ERROR = -1, RUNNING, TERMINATED };
extend enum ThreadState { CANCELED };
Thanks in advance.
Yes, it could be useful, but... ;-)
First, when extending an enumeration type you're up agains the "Is a
circle an ellipse" conundrum. The answer is, a const circle is a const
ellipse. For a non-const circle it's not so; consider
enum ThreadState{ error = -1, running, terminated };
void foo( ThreadState state )
{
assert( error <= state && state <= terminated );
// As best we knew when this code was written.
// Yee ha!
}
extend enum ThreadState { cancelled };
int main()
{
ThreadState state = cancelled; // No warning possible.
foo( state ); // No warning possible.
}
Second, enumerations are generally -- not in all cases but generally
-- a sign of purely procedural non-OO code, which could much better be
recast at the design level in some OO way than focusing on language
level patches to the some of the mildest symptoms the code exhibits.
By the way, all uppercase should be reserved for macro names.
All uppercase for macros is an old convention dating back at least to
K&R C, wherefrom Java picked it and associated it with a slightly
different meaning, incompatible with the C and now C++ rationale, so the
above is indicative of Java background -- don't do that in C++ code.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]