About enums in C++

Started by
3 comments, last by maverick20 17 years, 6 months ago
Where do c++ programmers usally write thier ENUM types? lets say i got 5 enums, do i need to put each in diffrent header? Color.h , something1.h , something2.h ? enum Color { Red, Black }; enum something1 { ..., .... }; enum something2 { ..., .... }; enum something3 { ..., .... }; enum something4 { ..., .... };
True knowledge exists in knowing that you know nothing.
Advertisement
Thats a very vague question to which there isn't really an answer.
I would say it depends on scope and relativeness to other enum types.
So do the enum want to be global, in namespace, in a class?
Is there any tie betweeen the enums or are you just polluting the scope?
You don't *need* to do it any particular way. If you really felt the need, you could put absolutely everything into a single source file. That would be a really bad idea, but you still could [smile]

Generally an enum doesn't make much sense on its own, it's usually associated with something. That something could be a set of flags to be passed to a function, for example. So the logical thing to do is to keep your enum in the same headers as that 'something' that it's associated with.
"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The only verdict is vengeance; a vendetta held as a votive, not in vain, for the value and veracity of such shall one day vindicate the vigilant and the virtuous. Verily, this vichyssoise of verbiage veers most verbose, so let me simply add that it's my very good honor to meet you and you may call me V.".....V
yeah, there really isn't one specific way to arrange your enums. Logcally as joanusdmentia has said, an enum that is part of a class and/or function should logically be in the same header as that class/function. But of course not all enums are associated with one specific class/func. For example an enum that enumerates the keyboard keys - which is quite common should probably get it's own header file. Because this enum will be necessary in many different source files. But then again, maybe it's just part of your one window class and since all key presses are filtered in through the window class, you just need it in the header corresponding with your window class... So there's a valid case for both.

In short, do what you think feels right.
[size=2]aliak.net
Welll...i think u should use one header for it if you are writing an specific application.....

This topic is closed to new replies.

Advertisement