Re: Should WM_USER-like things be defines or consts?
On Dec 9, 5:01 pm, Jonathan Lee <cho...@shaw.ca> wrote:
On Dec 9, 6:00 am, "dragan" <spambus...@prodigy.net> wrote:
It's a question about defining manifest constants (at least
I think that the WM_USER-like stuff fits that definition).
For things where the value of the constant is irrelevant, but
I need uniqueness, I prefer enums. So WM_XXX events would fit
in this category for me. It's less maintenance, and I don't
feel compelled to use all-capitals for the name, which I find
ugly.
Typically, all-capitals are reserved for macros, which don't
obey scope.
Then I put the enum in a namespace. Something like
namespace Event {
enum event_t { Quit, Paint, Move, Resize, User };
}
If the user is free to create event_t's, they do so as ints,
ex., (Event::User + 1). Then, whatever interface I've written
accepts ints. For example, an event loop that has a
postEvent(int) function.
It's also possible to reserve a zone of values in an enum. You
don't want to use int's unless absolutely necessary, since you
loose type checking.
On the other hand, when the value is meaningful (such as a bit
mask) then I'll use a static const global. Unless it's
something I want to be seen from other translation units, but
that doesn't happen often for me.
C++ has defined enum's so that they can be used for bit masks as
well. With, again, strict type checking. (But you have to
overload the | and & operators.)
--
James Kanze