Re: Is extended const common practice?
On 5/27/2010 12:15 PM, DeMarcus wrote:
[..] My real idea is that I want
to create some form of message id that would look like this.
const MessageID( 4711, "GetStatus" );
const MessageID( 4712, "SetStatus" );
I am guessing you omitted the names of the objects. Did you mean to write
const MessageID msgGetStatus(4711, "GetStatus");
const MessageID msgSetStatus(4712, "SetStatus");
?
Now the integer could be used internally to avoid strcmp for performance
reasons, and the string can be used for debugging and when sending the
message over the net.
Does the constant's value, so to speak, like 4711, get used in some form
other than just returning it from a function? I mean, PI and E are used
in expressions, where mnemonic designation is preferred over the number.
If you only use those constants to pass around, you might consider
investing in a mechanism that would translate the constant's value into
the "meaning". Then you will have a single place to provide any other
fancy processing, like translation into another language.
Take Windows applications for example. Help IDs are just numbers. Yet
there is a system that "converts" those numbers into a readable document
that a special application can display for you. The same with error
messages that are mostly identified by their, well, ID, which is, again,
just a number. The textual content is then obtained from the "resource"
system (which can actually be independently compiled and linked to the
existing executable without having to change the code).
As you say, the integers are likely to change when more messages are
added, hence version problems may arise if compiled into the code.
If you dedicate 32 bits unsigned integers to those IDs, you aren't going
to run out of numbers any time soon. Four billion is quite a number.
Learn more about what's available out there before committing to anything.
V
--
I do not respond to top-posted replies, please don't ask